Addresses in a sed script can be in any of the following forms:
1~2; to pick every third line starting with the second, ‘2~3’ would be used; to pick every fifth line starting with the tenth, use ‘10~5’; and ‘50~0’ is just an obscure way of saying
/characters, each must be escaped by a backslash (
The empty regular expression ‘//’ repeats the last regular
expression match (the same holds if the empty regular expression is
passed to the
s command). Note that modifiers to regular expressions
are evaluated when the regular expression is compiled, thus it is invalid to
specify them together with the empty regular expression.
%may be replaced by any other single character.)
This also matches the regular expression regexp,
but allows one to use a different delimiter than
This is particularly useful if the regexp itself contains
a lot of slashes, since it avoids the tedious escaping of every
If regexp itself includes any delimiter characters,
each must be escaped by a backslash (
Imodifier to regular-expression matching is a GNU extension which causes the regexp to be matched in a case-insensitive manner.
Mmodifier to regular-expression matching is a GNU sed extension which causes
$to match respectively (in addition to the normal behavior) the empty string after a newline, and the empty string before a newline. There are special character sequences (
\') which always match the beginning or the end of the buffer.
Mstands for multi-line.
If no addresses are given, then all lines are matched; if one address is given, then only lines matching that address are matched.
An address range can be specified by specifying two addresses
separated by a comma (
,). An address range matches lines
starting from where the first address matches, and continues
until the second address matches (inclusively).
If the second address is a regexp, then checking for the ending match will start with the line following the line which matched the first address: a range will always span at least two lines (except of course if the input stream ends).
If the second address is a number less than (or equal to) the line matching the first address, then only the one line is matched.
GNU sed also supports some special two-address forms; all these are GNU extensions:
0can be used in an address specification like
/so that sed will try to match regexp in the first input line too. In other words,
/is similar to
/, except that if addr2 matches the very first line of input the
/form will consider it to end the range, whereas the
/form will match the beginning of its range and hence make the range span up to the second occurrence of the regular expression.
Note that this is the only place where the
0 address makes
sense; there is no 0-th line and commands which are given the
address in any other way will give an error.
! character to the end of an address
specification negates the sense of the match.
That is, if the
! character follows an address range,
then only lines which do not match the address range
will be selected.
This also works for singleton addresses,
and, perhaps perversely, for the null address.