www.delorie.com/gnu/docs/sed/sed_4.html   search  
 
Buy the book!


sed, a stream editor

[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

3.1 Selecting lines with sed

Addresses in a sed script can be in any of the following forms:

number
Specifying a line number will match only that line in the input. (Note that sed counts lines continuously across all input files unless `-i' or `-s' options are specified.)

first~step
This GNU extension matches every stepth line starting with line first. In particular, lines will be selected when there exists a non-negative n such that the current line-number equals first + (n * step). Thus, to select the odd-numbered lines, one would use 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.

$
This address matches the last line of the last file of input, or the last line of each file when the `-i' or `-s' options are specified.

/regexp/
This will select any line which matches the regular expression regexp. If regexp itself includes any / 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%
(The % 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
The 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
The 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
Start out in "matched first address" state, until addr2 is found. This is similar to `1,addr2', except that if addr2 matches the very first line of input the 0,addr2 form will be at the end of its range, whereas the 1,addr2 form will still be at the beginning of its range.

addr1,+N
Matches addr1 and the N lines following addr1.

addr1,~N
Matches addr1 and the lines following addr1 until the next line whose input line number is a multiple of 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