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!

System Admin Handbook

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

This book approaches system administration in a practical way and is an invaluable reference for both new administrators and experienced professionals. It details best practices for every facet of system administration, including storage management, network design and administration, email, web hosting, scripting, and much more.

$ Check price
74.9974.99Amazon 4.5 logo(142+ reviews)

More Linux & UNIX resources

Aching back from coding all day?

Prism Glasses

Aching back from coding all day? Try Back Problems

Ever feel achy from sitting crunched up on your computer table? Try lying down with these optical glasses that allow you to work on your laptop while lying flat on your back. This is the perfect solution with those with limited mobility or those who wish to prevent neck cramps and back strains.

$ Check price
4.454.45Amazon 4 logo(128+ reviews)

More Back Problems resources