Sed

From Secure Computing Wiki
Jump to: navigation, search

Great reference: http://www.grymoire.com/Unix/Sed.html#uh-0 GOLD: sed 1-liners

  • 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>