Wednesday, June 10, 2009

Couple of good tricks with SED

I've gathered couple of sed examples I find myself using again and again.

Replace instances in string "foo" with "bar" in a file:
#sed -i 's/foo/bar/g' file.txt

Delete blank lines from the document 'grep1.txt' and creates 'sed1.txt'
#sed -e '/^$/d' grep1.txt > sed1.txt

Remove a trailing character:
#sed 's/.$//'

Print all text after certain string:
#cat Document|sed '/STRING/,// !d'

Comment out lines containing "console output" string:
#sed -i 's/^\(console output\)$/#\1/1' file.txt

Delete all blank spaces until first character on each line:
#sed -e 's/^[ \t]*//' file.txt