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!
Relieve spasms, tight muscles, trigger points and pressure points with the Body Back Buddy! This trigger point massage is designed to help you self-message any area of your body - especially those that are hard to reach. Keeping your muscles relaxes and out of contraction is importan in helping to reduce pain and prevent muscle injury.$ 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