You may have seen
sed programs even if you didn’t know that’s what they were. In online discussions it’s common to hear someone say
as a shorthand to mean “replace foo with bar.” The line
s/foo/bar/ is a complete
sed program to do such a replacement.
sed comes with every Unix-like operating system and is available for Windows here. It has a range of features for editing files, but
sed is worth using even if you only know how to do one thing with it:
sed "s/pattern1/pattern2/g" file.txt > newfile.txt
This will replace every instance of pattern1 with pattern2 in the file
file.txt and will write the result to
newfile.txt. The original file
file.txt is unchanged.
I used to think there was no reason to use
sed when other languages like Python will do everything
sed does and much more. Suppose you agree with that. Now suppose you find you often have to make global search-and-replace operations and so you write a script to do this, say a Python script. You’ve got to call your script something, remember what you called it, and put it in your path. How about calling it
sed? Or better, don’t write your script, but pretend that you did. If you’re on Linux, it’s already in your path. One advantage of the real
sed over your script named
sed is that the former can do a lot more, should you ever need it to.
Now for a few details regarding the
sed command above. The “s” on the front stands for “substitute” and the “g” on the end stands for “global.” Without the “g” on the end,
sed would only replace the first instance of the pattern on each line. If that’s what you want, then remove the “g.”
The patterns inside a
sed command are regular expressions, so it’s best to get in the habit of always quoting
sed commands. This isn’t necessary for simple string substitutions, but regular expressions often contain characters that you’ll need to prevent the shell from interpreting.
You may find the default regular expression support in
sed, you can add the
-r option for more familiar regular expression syntax.
I got the idea for this post from Greg Grouthaus’ post Why you should learn just a little Awk. He makes a good case that you can benefit from learning just a few commands of a language like Awk with no intention to learn more of the language.
More regular expression posts
For daily tips on regular expressions, follow @RegexTip on Twitter.