Creating sed Utilities
As you've seen in the short examples presented so far in this chapter, you can do lots of cool data-formatting things with the sed
editor. This section shows a few handy well-known sed
editor scripts for performing common data-handling functions.
Spacing with double lines
To start things off, look at a simple sed
script to insert a blank line between lines in a text file:
$ sed 'G' data2.txt
This is the header line.
This is the first data line.
This is the second data line.
This is the last line.
$
That was pretty simple! The key to this trick is the default value of the hold space. Remember that the G
command simply appends the contents of the hold space to the current pattern space contents. When you start the sed
editor, the hold space contains an empty line. By appending that to an existing line, you create a blank line after the existing line.
You may have noticed that this script also adds a blank line to the last line in the data stream, producing...