From Secure Computing Wiki
Revision as of 18:19, 31 March 2009 by Ecrist
- Find foo and replace it with bar on the first line only, edit in place:
sed -i "" "1 s/foo/bar/" <filename>
- Find all lines containing foo and remove those lines from file <filename>:
sed -i '' -e 's/^.*foo.*//' -e '/^$/d' <filename>
sed -i -e '/foo/d' <filename>
- Find ALL instances of foo and replace it with bar within a file <filename>.
sed -i -e 's/foo/bar/' <filename>
- Replace all instances of foo with bar on lines containing baz within file <filename>.
sed -e '/baz/s/foo/bar/' <filename>
- Find ALL instance of foo and replace it with bar within a file <filename>, and create a backup of the original file.
sed -i '.bak' 's/foo/bar/g' <filename>