Sed

From Secure Computing Wiki
Revision as of 12:41, 13 February 2008 by Ecrist (Talk | contribs)

Jump to: navigation, search
  • 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>
  • Find the first instance of foo and replace it with bar within a file <filename>. This is really good for multi-gigabyte files where you want to change something on the first line without loading the file into an editor.
sed -i "" "s/foo/bar/;q" <filename>
  • Find ALL instances of foo and replace it with bar within a file <filename>.
sed -in "s/foo/bar/g" <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" -n "s/foo/bar/g <filename>