|Previous||Table of Contents||Next|
The sed command supports four types of addresses. These addresses are used to match the text in the pattern space. If a match occurs based on the given address, the edit commands following the address are applied to the pattern space. The four types of addresses are:
|no address||If no address is given, the editing command is applied to every pattern space (input line).|
|number||If a number is given for an address, the editing command is applied to the pattern space whose input line number is equal to the given address number. If multiple input files are listed, the input lines are numbered consecutively from one file to the next. Therefore, there is only one line number 1.|
|$||The dollar sign signifies the last line of input. If more than one file is given, then $ refers to the last line of the last file.|
|context||A context address is a regular expression supported by the ed command. The regular expression is placed between two delimiters; normally the delimiters are slashes. For example,|
|sed -e '/pie/s/Apple/Cherry/' fruits|
|searches for an input line that contains the pattern "pie." If "pie" is found, the pattern space is selected and all edit commands are applied.|
|You can change the delimiters for context addresses if you wish. Start the address with \c, where c is the character you want to use as the delimiter. The next character c is used as the second delimiter. To use c within the regular expression you must escape its meaning with a backslash. For example,|
|sed -n '\+Two\+Two+p' inputfile|
|matches the pattern "Two+Two."|
|The context address may contain a new-line character. Unlike the ed pattern selection, the sed context pattern can contain the "\n" notation to match a new-line in the pattern space. Initially, the pattern space contains only one new-line which cannot be matched by \n. But you can use editing commands to add additional lines to the pattern space, thereby placing embedded new-lines in the pattern space.|
|A period (.) matches any character except the new-line.|
Any of the four types of addresses can be combined to form an address range. An address range matches all input lines from the first address through the second address. For example,
sed -e '/Apple/,/Orange/s/Pie/Jam/' fruit
would match all lines from the first line containing "Apple" to the first line containing "Orange" and change "Pie" to "Jam."
Using numbers for address ranges might resemble
sed -e '5,10s/Pie/Jam/' fruit
which would change Pie to Jam on lines 5 through 10.
There are three forms of addressing used by editing commands. The forms are:
|no address||If no address is given then every pattern space (input line) is selected.|
|single address||If a single address is given, then only pattern space text matching the given address is edited. For example, if you specify a line number, then only that input line is edited. If you specify a context address, then only those lines containing the regular expression are edited.|
|two addresses||If two addresses are specified, then all lines inclusive of the two addresses are edited. For example, if you specify|
|sed -e '1,5s/it/the dog/g' inputfile|
|lines 1 through 5 are edited. See the previous section describing Address Ranges.|
The editing commands used by sed resemble those of the ed editor. Some of the commands are borrowed from ed, others are added for sed's special use. The editing commands can be specified on the command line using the -e option or placed in files. These commands are referred to as sed scripts. Multiple scripts can be defined on the command line by enclosing them in quotes or by using the -e option repeatedly. More than one edit command can be placed in a scriptfile.
These editing commands can be split into three categories. Those that perform text manipulation on the pattern space, those that add new text to the output, and those that provide flow control.
The following list describes the editing commands supported by sed. The addresses supported by each edit command varies. To show the maximum number of addresses a command supports, we use the [n] notation for the address, where n is the maximum number of addresses. For example,
|No address means you cannot use an address with the edit command.|
|||One address can be used, for example,|
|The first command deletes all lines containing "John," the second deletes line 5.|
|||Two addresses may be used. The addresses must be separated with a comma. For example,|
|The first line deletes all lines from the first occurrence of "John" to the first occurrence of "Mark." The second line deletes lines 10 through 25.|
|The a command appends text to the output after the current pattern space is written but before reading the next line of input. The text is not placed in the pattern space, thus the text is not modified by later edit commands. For example, if you have the following input|
|This is line 1
This is line 2
|and the sed statement is|
This is appended text.' input
|the output would be|
|This is line 1.
This is appended text.
This is line 2.
This is appended text.
|b label||Branches (jumps) to the specified :label command. The label can be from one to eight characters in length. If no label is given, then you branch to the end of the sed script. This allows conditional editing scripts.|
|Deletes the contents of the pattern space and sends the text to the output. All remaining editing commands are skipped since the pattern space is empty.|
|d||Deletes the contents of the pattern space. All remaining editing commands are skipped since the pattern space is empty.|
|D||Deletes the segment of text preceding the first embedded new-line from the pattern space. Another command must first be used to place multiple lines in the pattern space. All remaining editing commands are skipped even though there is text in the pattern space. The pattern space is not displayed to the output. It can, however, be saved to the hold space. If no other command, such as N, has been used, then D functions like the d command.|
|g||Replaces the contents of the pattern space with the contents of the hold space.|
|G||Appends the contents of the hold space to the contents of the pattern space.|
|h||Replaces the contents of the hold space with the contents of the pattern space.|
|H||Appends the contents of the pattern space to the contents of the hold space.|
|Writes the text to the standard output before the current pattern space is written to the standard output. The text is not placed in the pattern space; therefore, it is not affected by the remaining editing commands. For example, if your input was the same as the input used for command a and the sed statement was|
This is inserted text.' input
|the output would be|
|This is inserted text.
This is line 1.
This is inserted text.
This is line 2.
|l||Write (list) the contents of the pattern space on the standard output, substituting nonprintable characters (control characters) with two-digit ASCII codes. Lines longer than 80 columns are wrapped onto the next line. sed only supports ASCII character codes up to octal 177 (Delete); it does not support extended ASCII.|
|n||Display the contents of the pattern space on the standard output. Replaces the pattern space with the next line of input. This allows you to read in lines of text and send them to the standard output without being edited by the other edit commands in the script.|
|N||Appends the next input line to the pattern space. The two lines of text are separated with a new-line. The line number is increased by one.|
|p||Writes (print) the pattern space on the standard output. By default sed copies the pattern space to the standard output. Thus when you specify the p edit command, each line is printed (displayed) on the standard output twice, unless you specify the -n option on the command line.|
|P||Writes (Prints) the segment of text preceding the first new-line from the pattern space. The P command functions like the p command if a command such as N has not been used. The P command allows you to add multiple lines of input to the pattern space, display the first line, and then manipulate the pattern space as desired.|
|q||Quit sed. Branches to the end of the script and stops processing input (exits sed).|
|r rfile||Reads the entire contents of rfile and writes them to the standard output before reading the next line of input. The format of this command must be r followed by one space or one tab and the name of the rfile, with nothing after the filename.|
|s/RE/rs/flags||Substitute the replacement string rs for the regular expression RE. See the regular expression section below for a list of supported REs. The / (slash) may be replaced by any other character. A flag may be zero or more of the following:|
|g||Substitutes rs for all occurrences of RE in the pattern space. A global line substitute instead of a first occurrence substitute. The substitution is nonoverlapping, which means sed scans for the first occurrence, performs the substitution, then searches for the next occurrence. For example, if the pattern space contained "meesseessippi," the command s/e.s/is/g would result in "mississippi" even though e.s matches ees and ess. The ess never gets changed because the ees changes to iss before the ess is located.|
|n||Substitute only the nth occurrence of the RE with rs. The value of n can be from 1 to 512.|
|Previous||Table of Contents||Next|