100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
Programming

Multiline Processing

Learn how sed handles multiple lines at once using the pattern space, the hold space, and the D, P, N, H, and G commands to perform edits that span line boundaries.

Advanced sedAdvanced11 min readJul 10, 2026
Analogies

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

bash
# 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 text

The 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

Was this page helpful?

Topics covered

#Programming#SedStreamEditorStudyNotes#MultilineProcessing#Multiline#Processing#Beyond#Single#StudyNotes#SkillVeris#ExamPrep