A sed program consists of one or more lines with the following syntax:
[address[ ,address] ] instruction [argument-list]
The addresses are optional. If you omit the address, sed processes all lines from the input. The instruction is the editing instruction that modifies the text. The addresses select the line(s) that the instruction part of the command operates on. The number and kinds of arguments in the argument-list depend on the instruction. If you want to put several sed commands on one line you can separate the commands with semicolons (;).
The sed utility processes input as follows:
Reads one line of input from file-list or standard input.
Reads the first instruction from the program or program-file. If the address(es) select the input line, acts on the input line as the instruction specifies.
Reads the next instruction from the program or program-file. If the address(es) select the input line, acts on the input line (possibly modified by the previous instruction) as the instruction specifies.
Repeats step 3 until it has executed all instructions in the program or program-file.
Starts over again with step 1 if there is another line of input; otherwise, it is finished.
A line number is an address that selects a line. As a special case, the line number $ represents the last line of input.
A regular expression (refer to Appendix A) is an address that selects those lines containing a string that the regular expression matches. Although slashes are often used to delimit these regular expressions, sed permits you to use any character other than a backslash or NEWLINE for this purpose.
Except as noted, zero, one, or two addresses (either line numbers or regular expressions) can precede an instruction. If you do not use an address, sed selects all lines, causing the instruction to act on every line of input. Providing one address causes the instruction to act on each input line that the address selects. Providing two addresses causes the instruction to act on groups of lines. The first address selects the first line in the first group. The second address selects the next subsequent line that it matches; this line is the last line in the first group. If no match for the second address is found, the second address points to the end of the file. After selecting the last line in a group, sed starts the selection process over again, looking for the next line that the first address matches. This line is the first line in the next group. The sed utility continues this process until it has finished going through the entire file.
(delete) The Delete instruction causes sed not to write out the lines it selects and not to finish processing the lines. After sed executes a Delete instruction, it reads the next input line and begins over again with the first instruction from the program or program-file.
(next) The Next instruction writes out the currently selected line if appropriate, reads the next input line, and starts processing the new line with the next instruction from the program or program-file.
(append) The Append instruction appends one or more lines to the currently selected line. If you precede an Append instruction with two addresses, it appends to each line that is selected by the addresses; earlier versions of sed did not accept Append instructions with two addresses. If you do not precede an Append instruction with an address, it appends to each input line. An Append instruction has the following format:
| || |
[address[,address]] a\ text\ text\ ... text
You must end each line of appended text, except the last, with a backslash (the backslash quotes the following NEWLINE). The appended text concludes with a line that does not end with a backslash. The sed utility always writes out appended text, regardless of whether you use a n flag on the command line. It even writes out the text if you delete the line to which you are appending the text.
(insert) The Insert instruction is identical to the Append instruction except that it places the new text before the selected line.
(change) The Change instruction is similar to Append and Insert except that it changes the selected lines so that they contain the new text. When you specify an address range, Change replaces the entire range of lines with a single occurrence of the new text.
(substitute) The Substitute instruction in sed is similar to that in vim (page 166). It has the following format:
| || |
[address[,address]] s/pattern/replacement-string /[g][p][w file]
The pattern is a regular expression (refer to Appendix A) that is delimited by any character other than a SPACE or NEWLINE, traditionally a slash ( / ). The replacement-string starts immediately following the second delimiter and must be terminated by the same delimiter. The final (third) delimiter is required. The replacement-string can contain an ampersand (&), which sed replaces with the matched pattern. Unless you use the g flag, the Substitute instruction replaces only the first occurrence of the pattern on each selected line.
The g (global) flag causes the Substitute instruction to replace all nonoverlapping occurrences of the pattern on the selected lines.
The p (print) flag causes sed to send all lines on which it makes substitutions to standard output. This flag overrides the n option on the command line.
The w (write) flag is similar to the p flag but it sends its output to the file specified by file. A single SPACE and the name of the output file must follow a w flag.
(print) The Print instruction writes the selected lines to standard output, writing the lines immediately, and does not reflect the effects of subsequent instructions. This instruction overrides the n option on the command line. (The n option prevents sed from copying lines to standard output.)
(write) This instruction is similar to the Print instruction except that it sends output to the file specified by file. A single SPACE and the name of the output file must follow a Write instruction.
(read) The Read instruction reads the contents of the specified file and appends it to the selected line. A single SPACE and the name of the input file must follow a Read instruction.
(quit) The Quit instruction causes sed to terminate immediately.
(NOT) Causes sed to apply the following instruction, located on the same line, to each of the lines not selected by the address portion of the instruction. For example, 3!d deletes all lines except line 3 and $!p displays all lines except the last.
(group instructions) When you enclose a group of instructions within a pair of braces, a single address (or address pair) selects the lines on which the group of instructions operates. Use semicolons (;) to separate multiple commands appearing on a single line.
The sed info page lists the branch instructions as "Commands for sed gurus" and suggests that if you need them you might be better off writing your program in awk or Perl.
Identifies a location within a sed program. The label is useful as a target for the b and t branch instructions.
Unconditionally transfers control (branches) to label. Without label, skips the rest of the instructions for the current line of input and reads the next line of input (page 565).
Transfers control (branches) to label only if a Substitute instruction has been successful since the most recent line of input was read (conditional branch). Without label, skips the rest of the instructions for the current line of input and reads the next line of input (page 565).
The Pattern Space and the Hold Space
The sed utility has two buffers. The commands reviewed up to this point work with the Pattern space, which initially holds the line of input that sed just read. The Hold space can hold data while you manipulate data in the Pattern space; it is a temporary buffer. Until you place data in the Hold space it is empty. This section discusses commands that move data between the Pattern space and the Hold space.
Copies the contents of the Hold space to the Pattern space. The original contents of the Pattern space is lost.
Appends a NEWLINE and the contents of the Hold space to the Pattern space.
Copies the contents of the Pattern space to the Hold space. The original contents of the Hold space is lost.
Appends a NEWLINE and the contents of the Pattern space to the Hold space.
Exchanges the contents of the Pattern space and the Hold space.