manpagez: man pages & more
info sed
Home | html | info | man

File: sed.info,  Node: Range Addresses,  Next: Zero Address,  Prev: Regexp Addresses,  Up: sed addresses

4.4 Range Addresses
===================

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):

     $ seq 10 | sed -n '4,6p'
     4
     5
     6

   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).

     $ seq 10 | sed -n '4,/[0-9]/p'
     4
     5

   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:

     $ seq 10 | sed -n '4,1p'
     4

   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.

     The following examples demonstrate the difference between starting
     with address 1 and 0:

          $ seq 10 | sed -n '1,/[0-9]/p'
          1
          2

          $ seq 10 | sed -n '0,/[0-9]/p'
          1

‘ADDR1,+N’
     Matches ADDR1 and the N lines following ADDR1.

          $ seq 10 | sed -n '6,+2p'
          6
          7
          8

     ADDR1 can be a line number or a regular expression.

‘ADDR1,~N’
     Matches ADDR1 and the lines following ADDR1 until the next line
     whose input line number is a multiple of N.  The following command
     prints starting at line 6, until the next line which is a multiple
     of 4 (i.e.  line 8):

          $ seq 10 | sed -n '6,~4p'
          6
          7
          8

     ADDR1 can be a line number or a regular expression.

© manpagez.com 2000-2026
Individual documents may contain additional copyright information.