Sed

From Secure Computing Wiki
Revision as of 18:13, 31 March 2009 by Ecrist (Talk | contribs)

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