|
|
sed commands are applied one at a time in the order they appear (unless you change this order with one of the ``flow-of-control'' functions discussed in ``Functions''). sed works in two phases, compiling the editing commands in the order they are given, then executing the commands one-by-one on each line of the input file.
When sed begins to read the input file, it copies the first line of the file into its ``pattern space''. The pattern space is an area of memory used by sed to store the text which it is currently editing. sed then executes its list of commands, applying them to the contents of the pattern space.
sed only carries out those editing functions which correspond to an address matching the current pattern space. For example, if a command is specific to lines 50-150 of a file, sed will not carry it out if the pattern space does not contain one of those lines. (sed does not print any warnings when this happens: in general sed either silently ignores errors, or terminates abruptly with an error message.)
The first matching command is carried out on the text contained in the pattern space; then the second command is carried out on whatever remains in the pattern space, and so on. When no more commands remain to be executed, sed appends the contents of the pattern space to its output file and reads another line, then starts processing again, looping back to the first command.
Even if you change this default order of applying commands with one of the two flow-of-control functions, t and b, the input line to any command is still the pattern space resulting from the application of any previously executed commands.
You should also note that the range within which pattern matching occurs is normally one line of input text. If you need to carry out edits that cross line boundaries, you can read more than one line into the pattern space by using the N function described in ``Multiple input-line functions''.
The rest of this section discusses the principles of sed addressing, followed by a description of sed functions.