sed: Execution Cycle
6.1 How 'sed' Works
===================
'sed' maintains two data buffers: the active _pattern_ space, and the
auxiliary _hold_ space. Both are initially empty.
'sed' operates by performing the following cycle on each line of
input: first, 'sed' reads one line from the input stream, removes any
trailing newline, and places it in the pattern space. Then commands are
executed; each command can have an address associated to it: addresses
are a kind of condition code, and a command is only executed if the
condition is verified before the command is to be executed.
When the end of the script is reached, unless the '-n' option is in
use, the contents of pattern space are printed out to the output stream,
adding back the trailing newline if it was removed.(1) Then the next
cycle starts for the next input line.
Unless special commands (like 'D') are used, the pattern space is
deleted between two cycles. The hold space, on the other hand, keeps
its data between cycles (see commands 'h', 'H', 'x', 'g', 'G' to move
data between both buffers).
---------- Footnotes ----------
(1) Actually, if 'sed' prints a line without the terminating newline,
it will nevertheless print the missing newline as soon as more text is
sent to the same output stream, which gives the "least expected
surprise" even though it does not make commands like 'sed -n p' exactly
identical to 'cat'.