Skip to content
Tutorials Center
My learning

Topics

Lesson 11 of 38 ยท sed Scripting

The sed Hold Space: h, H, g, G and x

Understand sed's second buffer, the hold space: the five commands h, H, g, G and x, with worked examples such as reversing a file, swapping lines and remembering text.

sed normally forgets a line as soon as it has printed it. That is what lets it handle huge files, but it also means that a command on line 5 cannot see line 4. To remember something between lines, sed gives you a second buffer called the hold space. It starts empty and is yours to use. Five small commands move text between the two buffers, and with them you can reverse files, swap lines, collect paragraphs and look back at earlier lines.

Two buffers

Buffer Name Role
pattern space PS the current line; what you edit and what gets printed
hold space HS a scratch pad that persists between lines
flowchart LR
    IN["input line"] --> PS["pattern space"]
    PS -- "h copy / H append" --> HS["hold space"]
    HS -- "g copy / G append" --> PS
    PS <-- "x exchange" --> HS
    PS --> OUT["output"]

The five commands: h and H send, g and G fetch, x swaps.

Command Action
h copy PS into HS (overwrites HS)
H append newline + PS to HS
g copy HS into PS (overwrites PS)
G append newline + HS to PS
x exchange PS and HS

Mnemonic: lower case replaces, upper case appends; h and H point to the hold space, g and G get from it.

Watching the buffers move

The easiest way to learn is to run tiny scripts on a few lines.

Bash x swaps, G appends

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

sed G is the famous way to double-space a file: the hold space is empty, so G adds a newline and nothing else, making a blank line after each line. With x, each cycle puts the current line into the hold space and takes out the previous line, so the output lags one line behind (the very first line prints as an empty line, because the hold space started empty).

Example: reverse a file (tac)

TEXT
sed -n '1!G;h;$p'

Read it as: for every line except the first, append the hold space (all the earlier lines, reversed) to the current line (1!G); copy the result to the hold space (h); at the last line print ($p). After line 3, the pattern space is three\ntwo\none.

Bash tac with the hold space

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

$!d deletes every line but the last, so only the final accumulated buffer is printed. Both forms do the same.

Example: swap pairs of lines

TEXT
sed -n 'h;n;G;p'

Hold the odd line (h), read the even line (n replaces the pattern space with the next line, printing nothing because of -n), then append the held line (G) and print: even first, then odd.

Bash Swapping each pair

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

If the number of lines is odd, n has no next line and GNU sed prints the pattern space (without -n) and exits; with -n the last unpaired line is lost, so check your input.

Example: join two lines (key and value)

Many files alternate between a label line and a value line. N is the neater tool (see branching and multi-line editing), but the hold space also works:

Bash Join every pair of lines

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

h saves the label, n loads the value, H appends the value to the label in the hold space (label\nvalue), x brings it into the pattern space, s replaces the newline, and p prints.

Example: print the line before a match

Remember each line in the hold space; when the current line matches, print what you remembered:

Bash Show the line before every error

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

For every line: if it matches ERROR, x brings the previous line (saved last cycle) into the pattern space, p prints it, and x swaps back. The final h (outside the braces, so it runs on every line) saves the current line for the next cycle. GNU users can also say grep -B1 ERROR, but the sed version generalizes: you could edit the previous line, join it, and so on.

Example: move a line to the end of the file

To move the line that matches Footer to the end:

Bash Move a matching line to the end

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

When the footer line is seen it is saved (h) and deleted (d). On the last line, $G appends the hold space, which prints the footer after it. This works if the footer line comes before the end. A second pass or H could handle other cases.

Tips for reading hold-space scripts

  1. Track both buffers on paper for two or three input lines.
  2. Remember that G and H add a newline between the two parts. That is why s/\n/ / often follows.
  3. Initially the hold space is empty, so the first G adds a blank line; this is the reason for 1!G.
  4. Hold-space programs are compact but obscure. If a script needs more than a few commands, consider AWK instead (see AWK Basics), which holds state in named variables.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which command copies the pattern space into the hold space, replacing what was there?

    Lower case h overwrites the hold space with the pattern space; H appends.
  2. 2. What does sed G do to a file?

    The hold space is empty, so G appends a newline and nothing else, adding a blank line.
  3. 3. In sed -n '1!G;h;$p', what does 1!G mean?

    1! negates the address, so G runs on all lines except line 1 (the hold space is empty then).
  4. 4. What does x do?

    x swaps the two buffers.

Summary

  • The hold space is a persistent second buffer, empty at the start.
  • h/H copy/append the pattern space to it; g/G copy/append it back; x swaps.
  • G and H insert a newline between the pieces.
  • Classic uses: double spacing, reversing, swapping lines, looking back at the previous line.

Next: Branching, Loops and Multi-line Editing