| www.delorie.com/gnu/docs/sed/sed_4.html | search |
![]() Buy the book! | |
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
sed
Addresses in a sed script can be in any of the following forms:
number
sed counts lines continuously across all input files
unless `-i' or `-s' options are specified.)
first~step
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 50.
$
/regexp/
/ characters,
each must be escaped by a backslash (\).
Unless POSIXLY_CORRECT is set, 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 illegal to specify
them together with the empty regular expression.
If POSIXLY_CORRECT is set, instead, `//' is the null match:
this behavior is mandated by POSIX, but it would break too many legacy
sed scripts to blithely change GNU sed's default behavior.
\%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 new-line,
and the empty string before a new-line. There are special character
sequences
(\` and \'
in basic or extended regular expression modes)
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. As a GNU extension, a
line number of 0 can be used in an address specification
like `0,/regexp/' so that regexp will be
matched in the first input line too.
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:
0,addr2
addr1,+N
addr1,~N
Appending 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.
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
| webmaster donations bookstore | delorie software privacy |
| Copyright © 2003 by The Free Software Foundation | Updated Jun 2003 |