Combining a series of edits in a script can have unexpected results. You might not think of the consequences one edit can have on another. New users typically think that sed applies an individual editing command to all lines of input before applying the next editing command. But the opposite is true. sed applies every editing command to the first input line before reading the second input line and applying the editing script to it. Because sed is always working with the latest version of the original line, any edit that is made changes the line for subsequent commands. sed doesn't retain the original. This means that a pattern that might have matched the original input line may no longer match the line after an edit has been made.
Let's look at an example that uses the substitute command.
Suppose someone quickly wrote
the following script to change
The first command would change
This mistake is simply a problem of the order of the commands in
Reversing the order of the commands - changing
Some sed commands change the flow through the script. For example, the 34.13 and other articles after it.reads another line into the pattern space without removing the current line, so you can test for patterns across multiple lines. Other commands tell sed to exit before reaching the bottom of the script or to go to a labeled command. sed also maintains a second temporary buffer called the hold space . You can copy the contents of the pattern space to the hold space and retrieve it later. The commands that make use of the hold space are discussed in article
- from O'Reilly & Associates' sed & awk