Next: Regular Expressions, Previous: Execution Cycle, Up: sed Programs
Addresses in a sed script can be in any of the following forms:
~
step1~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 50
.
$
/
regexp/
/
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.
\%
regexp%
%
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 (\
).
/
regexp/I
\%
regexp%I
I
modifier to regular-expression matching is a GNU
extension which causes the regexp to be matched in
a case-insensitive manner.
/
regexp/M
\%
regexp%M
M
modifier to regular-expression matching is a GNU sed
extension which causes ^
and $
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
(\`
and \'
)
which always match the beginning or the end of the buffer.
M
stands 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:
0,/
regexp/
0
can be used in an address specification like
0,/
regexp/
so that sed will try to match
regexp in the first input line too. In other words,
0,/
regexp/
is similar to 1,/
regexp/
,
except that if addr2 matches the very first line of input the
0,/
regexp/
form will consider it to end the range, whereas
the 1,/
regexp/
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 0
address in any other way will give an error.
,+
N,~
NAppending the !
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.