Bitbull Tech Notes - home of free minds ...

Usefull sed ,awk and perl onliners

Reverse order lines:

cat logfile | sed '1!G;h;$!d'

unix2dos:

sed -i 's/$/\r/' file.txt

dos2unix:

sed -i 's/\r//' file.txt

search and replace in file:

sed -i 's/search/replace/g;' *.txt

delete block of text:

cat MYFILE |sed '/START_PATTERN/,/END_PATTERN/d'

remove color from bash output:

color-script.sh | col -b | sed 's/0;[0-9]*m//g'

Display a block of text:

awk '/start_pattern/,/stop_pattern/' file.txt

replace multiline pattern:

perl -i -pe 'BEGIN{undef $/;} s/START_PATTERN.*END_PATTERN/REPLACE_STRING/smg' file1.txt

show changelog from pending updates:

echo n | yum update --changelog | sed '1,/Changes in packages about to be updated:/d' | sed '/Running transaction check/,$d'

generate hosts file from domain transfer:

D=internal.bitbull.ch ;host -l $D | awk '{print $4 " "$1 }' | sed "s/.$D//g"
Home