04. Substituting (Search and Replace)

The substitution option is probably the most well-used feature of sed. Substitution takes in an optional line address, the 's' command, a delimiter, the pattern to search for, the replacement, and any flags. Seems like a lot going on for a one line-liner, but with practice, you'll see it's a cinch!

Anatomy of a sed substitution command
The anatomy of a sed substitution command.


We've already seen how line addresses work, so let's move onto the delimiter. This is the symbol used to separate the different components of our sed command. The interesting thing here is that we may use any character we want!

Canonically, we use the / character, but in the case we are matching filename paths (e.g. /usr/bin), it's common to use the semi-colon (;) instead.

If for some reason really want to keep consistent, we can escape the backslashes in our filesnames with the forward slash \. However, this ends up looking like a picket fence, which can be difficult to read.

$ sed 's/\/usr\/bin/\/usr\/local\/bin/g' file.txt
# Use a delimiter to make reading easier.
$ sed 's;/usr/bin;/usr/local/bin;g' file.txt

In summary, any character that comes after the 's' command is considered the delimiter.

Two regex's - the search and the replace

Now we may include two regular expressions - one used to find the pattern of interest (what to search for), and the other to replace.

$ sed 's/OS/ring/' oneOS.txt
One ring to rule them all, One ring to find them. One ring to call them all, And in salvation bind them. In the bright land of Linux, Where the hackers play.

Don't forget the last delimiter!

Make sure to add that third delimiter at the end of your sed command. If not, the command won't go through!

$ sed 's/OS/ring' oneOS.txt
sed: 1: "s/OS/ring": unterminated substitute in regular expression


After the two regular expressions, we can tack on an optional modifier. Think of these as a secondary option we can include per command.

Global replace with g

The most common modifier you'll come across is the global modifer. By default, sed only replaces the first occurrence per line. This means that if you have two or more occurrences of a pattern you would like to replace on a line, only the first will be edited.

$ echo "hello, hello, hello" | sed 's/hello/hi/'
hi hello hello
$ echo "hello, hello, hello" | sed 's/hello/hi/g'
hi hi hi

Remember that the file edits won't save in-place, unless you specify the -i option. Another way to save your output is to redirect it to some file. Refer back to the previous page for more information.

Printing lines where substitution took place with p

We may print just the lines where sed performed a substitution. Recall that the -n option is used to suppress all lines from outputting. By using this option with the p modifier, we can output only the lines where substitution took place.

$ sed -n 's/OS/ring/p' oneOS.txt
One right to rule them all, One ring to find them. One ring to call them all,

Ignoring case with I

Often times case-sensitivity does not matter. To match both upper and lower-case letters, use the I modifier.

$ sed 's/os/ring/I' oneOS.txt
One ring to rule them all, One ring to find them. One ring to call them all,

Note that this will not work on OS X's implementation of sed.

Take your Linux skills to the next level!

The Linux Command Line

Take your Linux skills to the next level! Try Linux & UNIX

The Linux Command Line takes you from your very first terminal keystrokes to writing full programs in Bash, the most popular Linux shell. Along the way you'll learn the timeless skills handed down by generations of gray-bearded, mouse-shunning gurus: file navigation, environment configuration, command chaining, pattern matching with regular expressions, and more.

$ Check price
39.9539.95Amazon 4.5 logo(274+ reviews)

More Linux & UNIX resources

Aching back from coding all day?

Inversion Therapy Table

Aching back from coding all day? Try Back Problems

Stretch out your back and relieve your back muscles with inversion therapy. This device counteracts the forces of gravity on the body by decompressing and elongating the spine. By using this product just ten minutes a day, you can be well on your way to improved circulation and posture while relieving muscle aches, back pain and stress.

$$ Check price
119.98119.98Amazon 4.5 logo(1,700+ reviews)

More Back Problems resources