08. The Hold Buffer/Space

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.

Figure of sed operations: read, execute, display. With Hold Buffer
How sed works with its hold buffer.

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.

Exchange with x

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.

  1. The first line is placed in the pattern space.
  2. The contents of the pattern space is exchanged with the contents of the hold buffer, which is empty as of now.
  3. The pattern buffer then replaces the current line with its contents which is nothing.
  4. Sed then makes its way to the next line, and stores this line into the pattern buffer.
  5. The pattern buffer is swapped with the hold buffer, which contains the line preceding it.
  6. The line within the pattern buffer is then outputted.
  7. Repeat from #4 until reaching the last line.
  8. On the last line, the second-to-last line will be in the hold buffer, and swapped with the pattern buffer.
  9. Since the last line will be held in the hold buffer, but is never swapped with the pattern buffer, it does not get printed out and sed finishes its execution. Remember that the hold buffer will not get ouputted unless explicitly requested.

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.

Hold with h or H

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 (\n) character.

Get with g or G

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.

Double spacing with G

Let's try the following example, which only makes use of the G command.

$ 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.

  1. The first line is placed in the pattern space.
  2. The holding space, which contains nothing, is appended with a newline character to the pattern space.
  3. The pattern space replaces the current line.
  4. Current line is outputted.
  5. This process is repeated until end of file.

This pretty simple right?

Reversing line order using sed

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.

$ 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. The first line is copied into the pattern space.
  2. 1!G says to apply the G command to all lines except the first, so we skip this one.
  3. The h command copies the contents into the holding space. The previous contents of the holding space are destroyed.
  4. The last command says print out just last line's pattern space, so we skip this one.
  5. Sed goes back to the first command, 1!G.
  6. The current line is placed within the pattern space.
  7. The contents of the holding space are appended to the pattern buffer with a newline character. On the nth iteration, this would cause the order to be: n, n-1 ... 2, 1.
  8. Repeat this from #2 until the last line.
  9. On the last line, print out the pattern space.

If you can understand the above, then you got the basics of sed!

Take your Linux skills to the next level!

System Admin Handbook

Take your Linux skills to the next level! Try Linux & UNIX

This book approaches system administration in a practical way and is an invaluable reference for both new administrators and experienced professionals. It details best practices for every facet of system administration, including storage management, network design and administration, email, web hosting, scripting, and much more.

$ Check price
74.9974.99Amazon 4.5 logo(142+ reviews)

More Linux & UNIX resources

Aching back from coding all day?

Foam Seat Cushion

Aching back from coding all day? Try Back Problems

This foam seat cushion relieves lowerback pain, numbness and pressure sores by promoting healthy weight distribution, posture and spine alignment. Furthermore, it reduces pressure on the tailbone and hip bones while sitting. Perfect for sitting on the computer desk for long periods of time.

$ Check price
99.9599.95Amazon 4.5 logo(9,445+ reviews)

More Back Problems resources

Ad