Earlier we introduced the pattern buffer, which is where a matched line is stored before it becomes manipulated.
There is another storage space known as the hold buffer which is used to copy or save the data in the pattern space for subsequent retrieval. The content held in the hold buffer can be exchanged or appended to the pattern buffer and vice versa.
There exists five operations that allow for manipulations between the pattern and hold buffers. However, no operations are available to be performed directly on the hold space.
Let's go over the five commands, then discuss how we can use the hold buffer to reverse a file.
The first command,
x, is used to exchange the current pattern and holding buffers. Try this simple command to see what happens:
$ sed 'x' oneOS.txt
One OS to rule them all, One OS to find them. One OS to call them all, And in salvation bind them. In the bright land of Linux,
Notice how the entire file got shifted down one line, and the last line was deleted. Here is a step-by-step analysis of what is happening.
Phew! Nine steps for one simple command. I may have overly broken it down, since this may a difficult concept to grasp for first-time users.
The hold function (
h) is used to copy the pattern buffer (line we are currently working on) into the hold buffer. The pattern buffer stays unchanged, while the previous contents of the holding buffer is destroyed.
If we wish to keep our hold buffer, and append to it, we can use the
H command. The pattern buffer is appended to the hold buffer, separated by a newline (
Now the reverse of the hold command is
g. This places what's in the hold buffer to the pattern space. This deletes the contents of the current pattern space.
Similarly, to append instead of overwriting, we use the
G command. This adds a new line to the pattern space, and appends the contents of the hold buffer.
Let's try the following example, which only makes use of the
$ sed 'G' oneOS.txt One OS to rule them all, One OS to find them. One OS to call them all, And in salvation bind them. In the bright land of Linux, Where the hackers play.
How does double-spacing work? Let's break it down step-by-step.
This pretty simple right?
Let's try one more trick to solidify our understanding of pattern and holding spaces. This three-tier command reverses the lines of a file. Remember that the semi-colon (
;) is used to separated commands in
$ sed -n '1!G;h;$p' oneOS.txt
Where the hackers play. In the bright land of Linux, And in salvation bind them. One OS to call them all, One OS to find them. One OS to rule them all,
1!Gsays to apply the
Gcommand to all lines except the first, so we skip this one.
hcommand copies the contents into the holding space. The previous contents of the holding space are destroyed.
If you can understand the above, then you got the basics of sed!
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
In this completely revised second edition of the perennial best seller How Linux Works, author Brian Ward makes the concepts behind Linux internals accessible to anyone curious about the inner workings of the operating system. Inside, you'll find the kind of knowledge that normally comes from years of experience doing things the hard way.$ Check price