Using sed

Feb 24, 2007

Many people don’t have a clue how to use old-school Unix commands such as sed and I can say that I was in that camp until recently. Previously, I would be more inclined to write a script in Perl or open up vi to perform complex and repetitive replacement tasks across multiple files. I’m comfortable with both Perl and vi, most importantly their regular expressions which allow for quick and easy manipulation of text. I was pushed to learn sed when I had a number of different files that all needed a very similar modification to each of their thousands of lines. Sure I could write a Perl script to do it, but it was a good opportunity to learn the stream editor and I’m glad I did.

The most useful thing sed can do is regex-based alterations to files just like vi, except you can apply a sed command to multiple files from the command line with a simple glob. The syntax for sed regex is much like that for vi or Perl; the big difference that I encountered as compared to Perl is that capturing with parenthesis is done using backslash-escaped parenthesis and the later references with backslash-escaped numbers. If you run sed without any options, it takes a regex and any number of input files; the output will be to standard out, so sed 's/foo/bar/' *.txt will replace ‘foo’ with 'bar’ in all text files in the current directory, showing you the results on STDOUT. This is great to test and make sure the right changes are going to be made, but to actually make the changes, the best method is to use sed’s in-place editing option, -i. Now, if you run sed -i 's/foo/bar/' *.txt, sed will replace 'foo’ with 'bar’ in all files in the current directory, altering each of the files.