What Is SED and How Do You Use It?

sed stands for stream editor and is a commonly-used command in Linux/Unix. The name comes from a portmanteau of those two words. It’s not a text editor, though it does modify text. Instead, sed receives text input as a “stream” and edits the stream according to your instructions.

By and large, people use sed as a command line version of find and replace. The main command in the program is used to substitute one character string for another. This makes it useful for running find and replace functions on a batch of files, but it is not as easy to use as something from Notepad or Word. You’ll find that the syntax takes a little getting used to, but once you have that down, you’ll see how incredibly powerful this compact editor can be.

To use sed, you’ll need to pop open a Terminal window. The command is invoked with sed, but typing that alone won’t do anything. You need to provide one of the commands, as well as some parameters.

The most common parameter is substitute, or s. A simple sed substitution command looks something like the following:

sed 's/cat/dog/'

That command will order sed to scan incoming text and replace every instance of the string “cat” with “dog.” To demonstrate, we’ll use the echo command to give sed something to work with.

echo cat catapult | sed 's/cat/dog/'

There are three main parts to the sed command. First, there’s sed which starts the program. Then there’s the s which starts the substitution command. The forward slash / is a separator. The string after that will be the string sed looks for. The string after the second slash will be the replacement characters that sed writes to the stream. The final slash closes the command. All together, it’s 's/text-to-find/text-to-replace/'.

Note the single quotes in that example. The single quotes allow you to use meta characters in your command. While quotes aren’t essential for most of our examples, you should develop the habit of using quotes for all commands.

What about partial strings? The above command will work on any appearance of the string “cat,” even if it’s part of another word. For example, “catapult” will become “dogapult” as “catamaran” will become “dogamaran.”

echo catapult | sed 's/cat/dog/'

In our first example you might have noticed only the first instance of the matching string was replaced. Like many GNU utilities, sed is line-based. As a result, it only operates on the first match within a line. If you have multiple matches for your candidate string in the line, only the first one will be changed. To make sed work on every occurrence of the target string, use the g flag at the end of the command:

sed 's/cat/dog/g'

This would operate like the following example:

echo cat catapult catamaran | sed 's/cat/dog/g'

Without the global flag, only the first instance of “cat” in each line would be replaced.

By default, sed will operate on standard input. However, that’s not too useful. To run sed on a file, specify the filename at the end of the command, like so:

sed 's/cat/dog/g' oldfile.txt > newfile.txt

This will run sed on “oldfile.txt” and save the command’s output to “newfile.txt.” If “newfile.txt” does not exist, it will be created by the command. If you don’t specify an output file, sed will echo the file’s new text into standard input.

If you want to overwrite the contents of a file, you’ll need to use the -i flag. This flag makes the edits “in place.”

sed -i '' 's/cat/dog' oldfile.txt

This command overwrites the contents of the target file immediately, which is slightly dangerous. To stay on the safe side, you can create a backup in-situ.

To create a backup of the file, put an extension after the -i. It doesn’t need to be a functional extension: “.bu” or “.bak” work fine. This will create a backup file with an extension.

sed -i.bak 's/cat/dog' oldfile.txt

This creates “oldfile.txt.bak,” which contains the unedited text data from the original version of “oldfile.txt.”

The real power of sed comes from matching regular expressions. This allows you to set patterns for matching, rather than literal strings. For example, we could use something like the string below to repeat any set of one or more numbers:

echo "123 abc" | sed 's/[0-9][0-9]*/& &/'

sed is an extremely powerful utility with a depth of power similar to a scripting language. To learn more about sed’s full capabilities, check out Bruce Barnett’s comprehensive guide to sed.