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!
Linux for Beginners doesn't make any assumptions about your background or knowledge of Linux. You need no prior knowledge to benefit from this book. You will be guided step by step using a logical and systematic approach. As new concepts, commands, or jargon are encountered they are explained in plain language, making it easy for anyone to understand.$ Check price
Relieve your stress, back, neck and sciatic pain through 1,782 acupuncture points for immediate neck pain relief. Made for lower, upper and mid chronic back pain treatment, and improves circulation, sleep, digestion and quality of life.$$ Check price