Sed

Preventing nasty escaping on shell

Instead of escaping all special characters for the shell as ; & and so on you can write

 sed -e 's/;/asdf/g' file.ext 

Stream editor sed

 sed -f sed-script.sed file > output.txt
 script | sed -f sed_script.sed > output

syntax:

 sed '[address1[,adress2]]command' [file(s)]

addresses

 sed -n '13,17p' file.dat
prints out from the 13. line up to the 17. line.
 sed -n '/pattern/p' file.dat
prints out all lines where pattern occur.

There is the possibility to give more than one command with syntax

 address{command1;command2;...}

 sed -n '{p; s/searchpattern/replacepattern/g ; p }' file.dat
prints out every line, then substitute searchpattern to replacepattern and prints it again.
 a append
 c change
 d delete
 g get buffer
 G get newline
 h hold buffer
 H hold newline
 i insert 
 l listing
 n next
 p print
 q quit
 r read
 s substitute
 x xchange
 y yank replace char y/sourcechar/replacechar/
 w write
 ! negation

options for s/.../.../option

 g global
 p output
 w swaps content of temp mem with selected line

examples:

 sed '5,$d' file.dat
deletes the lines from 5 to the end.
 sed '/pattern/d' file.dat
deletes all line where pattern occurs
 sed '/!pattern/d' fild.dat
deletes all lines where pattern does not occur.
 sed -n '/pattern/{p;q}' file.data
searchs for pattern prints this line and quits
 sed '/pattern/ s/searchpattern/replacepattern/' file.dat
replaces searchpattern with replacepattern in lines where pattern occurs.