Sed is available on all GNU/Linux distributions there is also a versions for Mac OS X too.
Typical usage of Sed are simple text replacement, testing for substrings, complex text replacement with regular expressions.
cat example.conf foo+bar=foo
sed -i 's/foo/foo_bar/g' example.conf
foo in file example.conf will be replaced with foo_bar
Start a dry run without changes, you will get a output about the changes (your file will stay untouched):
sed 's/192.168.0.1/nameserver.local/g' example.conf
Append after match:
sed '/PATTERN_TO_SEARCH/a APPEND_ME' my_file
Remove line number 134 from file:
sed -i 134d /root/.ssh/known_hosts
MAC OS X Section!
Sed inplace editing (-i) is a kind different on Mac OS X: in the normal UNIX case you will get a error:
id$ sed -i '/anna/ d' testfile sed: 1: "testfile": undefined label 'estfile'
Soo this is the right syntax:
sed -i '' '/anna/ d' testfile
By the way, this will delete all lines which contains “anna” ;)
From the manpage: you risk corruption or partial content in situ-ations where disk space is exhausted, etc.