The sed
stream editor is a powerful editing tool that can make sweeping changes with very little input. In the previous article on sed
, you explored the basics of using sed to edit text.
This article will continue your introduction by examining some more advanced topics.
Note: This tutorial uses the GNU version of sed
found on Ubuntu and other Linux operating systems. If you’re using macOS, you’ll have the BSD version which has different options and arguments. You can install the GNU version of sed
with Homebrew using brew install gnu-sed
.
To complete this tutorial, you’ll need some files to manipulate, which you should have from the the first tutorial. If you don’t have them, you can recreate them with the following commands:
Supplying Multiple Editing Sequences
There are quite a few instances where you might wish to pass multiple commands to sed
simultaneously. There are a few ways that this can be accomplished.
Since sed
operates through standard input and output, you can string different calls to sed
together through a pipeline. Execute this command to replace the word and
with an apersand (&
), and the word people
with horses
:
You’ll see the following output:
this is the song that never ends
yes, it goes on & on, my friend
some horses started singing it
not knowing what it was
& they'll continue singing it forever
just because...
This works, but it creates unnecessary overhead with multiple calls to sed
, requires more typing, and does not take advantage of sed
’s built-in capabilities.
You can string various commands to sed
by using the -e
option before each command. This is how you would rewrite the previous command:
Consider the =
operator. This operator inserts a line-number on a new line between each existing line. The output looks like this:
1
this is the song that never ends
2
yes, it goes on and on, my friend
3
some people started singing it
4
not knowing what it was
5
and they'll continue singing it forever
6
just because...
If you would like to change the formatting of the numbering by modifying the text, however, you’ll see that things do not work as expected.
To demonstrate, let’s look at the G
command, which by default, enters a blank line between each line (this actually is more complex, but you’ll explore that later):
this is the song that never ends
yes, it goes on and on, my friend
some people started singing it
not knowing what it was
and they'll continue singing it forever
just because...
If you combine these two commands, you might expect a space between each regular line and line-number line:
1
this is the song that never ends
2
yes, it goes on and on, my friend
3
some people started singing it
4
not knowing what it was
. . .
. . .
This happens because the =
operator modifies the actual output stream directly. This means that you cannot use the results for more editing.
You can get around this by using two sed
calls, treating the first sed
modification as a simple stream of text for the second:
1
this is the song that never ends
2
yes, it goes on and on, my friend
3
some people started singing it
. . .
. . .
Keep in mind that some of the commands operate like this, especially if you are stringing multiple commands together and the output differs from what you were expecting.
Advanced Addressing
One of the advantages of sed
’s addressable commands is that regular expressions can be used as selection criteria. This means that you are not limited to operating on known line values, like you saw previously:
Hello
Hello
Hello
not knowing what it was
and they'll continue singing it forever
just because...
You can, instead, use regular expressions to match only lines that contain a certain pattern. To do this, place the match pattern between two forward slashes (/) prior to giving the command strings:
this is the song that never ends
yes, it goes on and on, my friend
some people started singing it loudly
not knowing what it was
and they'll continue singing it loudly forever
just because...
In this example, you’ve placed loudly
after the first occurrance of it
on every line that contains the string singing
. Notice that the second and fourth line are unaltered because they do not match the pattern.
The expressions for addressing can be arbitrarily complex. This provides a great deal of flexibility in executing commands.
This is not a complex example, but it demonstrates using regular expressions to generate addresses for other commands. The following command matches any blank lines (the start of a line followed immediately by the end of the line) and passes them to the delete command:
GNU GENERAL PUBLIC LICENSE
Version 3, 29 June 2007
Copyright (C) 2007 Free Software Foundation, Inc.
Everyone is permitted to copy and distribute verbatim copies
of this license document, but changing it is not allowed.
Preamble
The GNU General Public License is a free, copyleft license for
. . .
. . .
Keep in mind that you can use regular expressions on either side of a range as well. For instance, you can delete lines starting at a line that only contains the word START
until a line reading END
,
For example, create a file called inputfile
:
This is an input file
This is additional text
One thing to note though, is that this will delete everything from the first START
to the first END
, and then restart the deletion if it comes across another START
marker.
If you want to invert an address (operate on any line that does not match a pattern), you can follow the pattern with an exclamation point (!
).
For example, you can delete any line that is not blank (not terribly useful, but just an example), with the following command:
The address does not need to be a complex expression to be inverted. Inversion works the same on regular numbered address too.
Using the Hold Buffer
One piece of functionality that increases sed
’s ability perform multi-line aware edits is what is called the “hold buffer”. The hold buffer is an area of temporary storage that can be modified by certain commands.
The presence of this extra buffer means that you can store lines while working on other lines, and then operate on each buffer as necessary.
The following are the commands that affect the holding buffer:
- h: Copies the current pattern buffer (the line you’re currently matched and working on) into the the holding buffer (this erases the previous contents of the hold buffer).
- H: Appends the current pattern buffer to the end of the current holding pattern, separated by a new-line (\n) character.
- g: Copies the current holding buffer into the current pattern buffer. The previous pattern buffer is erased.
- G: Appends the current holding pattern to the end of the current pattern buffer, separated by a new-line (\n) character.
- x: Swap the current pattern and holding buffers.
The contents of the holding buffer cannot be operated on until it is moved to the pattern buffer in one way or another.
Let’s explore this idea with a complex example.
This is a procedural example of how to join adjacent lines (sed
actually has a built-in command that would take care of a lot of this for us. The N
command appends the next line to the current line. You are going to do things the hard way though for the sake of practice):
this is the song that never ends yes, it goes on and on, my friend
some people started singing it not knowing what it was
and they'll continue singing it forever just because...
This is a lot to digest, so let’s break it down.
The first thing to note is that the -n
option is used to suppress automatic printing. sed
will only print when you specifically tell it too.
The first part of the instruction is 1\~2h
. The beginning is an address specification meaning to perform the subsequent operation on the first line, and then on every other line afterwards (each odd numbered line). The h
part is the command to copy the matched line into the holding buffer.
The second half of the command is more complex. Again, it begins with an address specification. This time, it is referring to the even numbered lines (the opposite of the first command).
The rest of the command is enclosed in braces. This means that the rest of the commands will inherit the address that was just specified. Without the braces, only the “H” command would inherit the address, and the rest of the commands would be executed on every line.
The H
command copies a new-line character, followed by the current pattern buffer, onto the end of the current holding pattern.
This holding pattern (an odd numbered line, followed by a new-line character, followed by an even numbered line) is then copied back into the pattern buffer (replacing the previous pattern buffer) with the g
command.
Next, the new-line character is replaced with a space and the line is printed with the p
command.
If you are curious, using the N
command would shorten this considerably. The following command will produce the same results that you’ve just seen:
As you begin to use more complex commands, it may be helpful to compose them in a text editor. This is also helpful if you have a large number of commands that you’d like to apply to a single target.
For example, if you like to compose messages in plain text, but you need to perform a set of standardized formatting before using the text, a sed
script would be useful.
Instead of typing each set of sed
calls, you can put the commands in a script and supply it as an argument to sed
. A sed
script is simply a list of raw sed
commands (the part normally between the single-quote characters).
To try this out, create a new file called sed_script
with the following contents:
Save the file and exit the editor.
Now tell sed
to use the file by using the -f
switch:
that is the song that never ends
yes, that goes on and on, my friend
some horses started singing that
not knowing what that was
and they'll continue singing that forever
just because...
This allows you to put all of your edits in one file and execute it on arbitrary text files that need to conform to the format you’ve created.