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!

Aching back from coding all day?

Prism Glasses

Aching back from coding all day? Try Back Problems

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
4.454.45Amazon 4 logo(128+ reviews)

More Back Problems resources

Take your Linux skills to the next level!

How Linux Works

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

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
39.9539.95Amazon 5 logo(114+ reviews)

More Linux & UNIX resources

Ad