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!
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 (
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.
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.
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.
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.
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,
Often times case-sensitivity does not matter. To match both upper and lower-case letters, use the
$ 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
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
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