Tag Archives: regex

Grep through files and replace

Want to replace recursively http://example.co.uk with http://10.0.0.7 and also create backups of the files modified: grep -rFl “http://www.example.co.uk” /path  | xargs sed -i.bak ‘s/http:\/\/www.example.co.uk/http:\/\/10.0.0.7/g’

Posted in Linux | Tagged , , | Leave a comment

Bash Assign Find Output To Array

To assign the output to an array, you need to use a command substitution inside of an array assignment. arr=($(grep -n “search term” file.txt | sed ‘s/:.*//’)) The inner $() runs the command while the outer () causes the output … Continue reading

Posted in Linux | Tagged , | Leave a comment

Escape special characters inside folder/file name variable

array=(`find /path/ -mindepth 2 -maxdepth 2 -type d -print | sed -e ‘s;[^/]*/;;g’ |  sed ‘s/&/\\&/’`) ( sed ‘s/&/\\&/’  replaces & with \& in the folder/file name )

Posted in Linux | Tagged , | Leave a comment

Remove/replace all characters before and after a specific character & Deal with “postfix/smtpd too many errors after UNKNOWN from unknown”

Plenty of logs on an e-mail server like the one below: Sep  3 05:31:01 web postfix/smtpd[7858]: too many errors after UNKNOWN from unknown[42.119.175.103] (These logs seem to be caused by botnets trying to send spam, or broken spam bots trying … Continue reading

Posted in Linux | Tagged , , | Leave a comment

Remove or replace all characters after character X

ls | sed -e ‘s/X.*//’    (remove) ls | sed -e ‘s/X.*/Y/’ (replace with Y)

Posted in Linux | Tagged , | Leave a comment

Remove spaces from files

find . -name ‘* *’ | while read file; do target=`echo “$file” | sed ‘s/ /_/g’`; echo “Renaming ‘$file’ to ‘$target’”; mv “$file” “$target”; done

Posted in Linux | Tagged | 1 Comment

Replace characters between 2 characters

Replace characters between C and F with XXX: perl -lpwe ‘s/C.*?F/XXX/g’

Posted in Linux | Tagged | Leave a comment

Add text to the end of a line

sed ‘s/$/Add Text to end/g’ myfile

Posted in Linux | Tagged | Leave a comment

Change the extension for multiple files

MP3 to mp3 ; jpg to JPG for f in *.MP3; do mv “$f” “`basename “$f” .MP3`.mp3″; done; or for f in *.jpg; do cp “$f” “`basename “$f” .jpg`.JPG”;  

Posted in Linux | Tagged | Leave a comment

Remove spaces from files

find . -name ‘* *’ | while read file; do target=`echo “$file” | sed ‘s/ /_/g’`; echo “Renaming ‘$file’ to ‘$target’”; mv “$file” “$target”; done

Posted in Linux | Tagged | Leave a comment