DOC HOME SITE MAP MAN PAGES GNU INFO SEARCH
 

(sed.info.gz) Addresses

Info Catalog (sed.info.gz) Execution Cycle (sed.info.gz) sed Programs (sed.info.gz) Regular Expressions
 
 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 (`\').
 
      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%'
      (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 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/'
      A line number of `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.
 
 `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.
 
Info Catalog (sed.info.gz) Execution Cycle (sed.info.gz) sed Programs (sed.info.gz) Regular Expressions
automatically generated byinfo2html