Beyond a Single Line
Most sed one-liners operate on a single line, but real text often needs edits that cross line boundaries: removing blank-line runs, reformatting records that span several lines, or reversing file order. sed supports this through two buffers — the pattern space, which holds the line currently being worked on, and the hold space, a secondary buffer you can stash data in. Multiline processing is the art of shuttling text between these two buffers with commands like N, D, P, H, h, G, g, and x.
Cricket analogy: The pattern space is the batsman on strike and the hold space is the non-striker at the other end; multiline work is rotating the strike so the right batsman faces each ball.
The P and D Commands
When the pattern space contains multiple lines (usually built up with N), the uppercase 'P' and 'D' commands operate only on the portion up to the first embedded newline. 'P' prints just that first line; 'D' deletes it and then, critically, restarts the cycle with the remaining text still in the pattern space instead of reading a new line. This D-restart behaviour is what makes sliding-window loops possible — you can keep pulling lines in with N and shedding processed ones with D.
Cricket analogy: 'P' is showing only the first ball of an over on replay, while 'D' is wiping that ball from the tape and rewinding to review the rest of the over without loading a new match.
Using the Hold Space
The hold space starts empty and persists across lines, making it perfect for accumulation, deferral, or reordering. 'h' copies the pattern space into the hold space (overwriting it); 'H' appends to it with a newline. 'g' copies the hold space back into the pattern space; 'G' appends it. 'x' exchanges the two buffers. A classic use is reversing a file's lines — the 'tac' behaviour — by appending each line to the hold space and dumping it in reverse at the end.
Cricket analogy: The hold space is the dressing room where batsmen wait; 'H' sends each dismissed batsman there and 'G' brings the whole line-up back out in reverse order for a highlights reel.
Squeezing Blank Lines and Reversing a File
# Collapse runs of blank lines into a single blank line.
sed '/^$/{ N; /^\n$/D }' file.txt
# Reverse the order of lines in a file (like tac), using the hold space.
sed -n '1!G; h; $p' file.txt
# 1!G : append hold space to pattern space (skip on line 1)
# h : copy pattern space to hold space
# $p : on the last line, print the accumulated reversed textThe tac-in-sed idiom 'sed -n "1!G; h; \$p"' works because each line is prepended to the growing hold-space contents: the current line goes into the pattern space, the previously accumulated (reversed) text is appended after it with G, and the result is stored back with h — so the newest line always ends up on top.
Multiline scripts can consume the whole file into memory. The tac idiom holds every line in the hold space until the last line, so on very large files it uses memory proportional to file size. For huge inputs prefer a streaming tool or the actual 'tac' command rather than accumulating everything in sed's buffers.
- sed has two buffers: the per-line pattern space and the persistent hold space.
- 'P' prints and 'D' deletes only up to the first embedded newline in the pattern space.
- 'D' restarts the cycle with the leftover text instead of reading a new line — enabling sliding windows.
- 'h'/'H' copy/append pattern space to hold space; 'g'/'G' copy/append hold space to pattern space.
- 'x' exchanges the pattern and hold spaces.
- Reversing a file uses '1!G; h; $p' to accumulate lines in reverse.
- Accumulating whole files in the hold space costs memory proportional to input size.
Practice what you learned
1. What is special about the 'D' command's behaviour after deleting?
2. Which command appends the pattern space to the hold space with a newline?
3. What does the 'x' command do?
4. In 'sed -n "1!G; h; $p"', why is G skipped on line 1?
5. What does the uppercase 'P' command print?
Was this page helpful?
You May Also Like
The n and N Commands
Understand how sed's lowercase n and uppercase N commands pull additional input lines into processing, and how they differ in what they do to the pattern space.
Branching and Loops in sed
Learn how sed uses labels, unconditional branches, and conditional branches to build loops and control flow inside its scripting language.
Backreferences and Groups
Master capturing parts of a match with groups and reusing them via backreferences in both the pattern and the replacement of sed substitutions.
Related Reading
Related Study Notes in Programming
Browse all study notesApache Spark Study Notes
Programming · 30 topics
ProgrammingApache Flink Study Notes
Programming · 30 topics
ProgrammingHadoop Study Notes
Programming · 30 topics
ProgrammingSnowflake Study Notes
Programming · 30 topics
ProgrammingApache Airflow Study Notes
Programming · 30 topics
Programmingdbt (Data Build Tool) Study Notes
Programming · 30 topics