sed: Range Addresses
1
1 4.4 Range Addresses
1 ===================
1
1 An address range can be specified by specifying two addresses separated
1 by a comma (','). An address range matches lines starting from where
1 the first address matches, and continues until the second address
1 matches (inclusively):
1
1 $ seq 10 | sed -n '4,6p'
1 4
1 5
1 6
1
1 If the second address is a REGEXP, then checking for the ending match
1 will start with the line _following_ the line which matched the first
1 address: a range will always span at least two lines (except of course
1 if the input stream ends).
1
1 $ seq 10 | sed -n '4,/[0-9]/p'
1 4
1 5
1
1 If the second address is a NUMBER less than (or equal to) the line
1 matching the first address, then only the one line is matched:
1
1 $ seq 10 | sed -n '4,1p'
1 4
1
1 GNU 'sed' also supports some special two-address forms; all these are
1 GNU extensions:
1 '0,/REGEXP/'
1 A line number of '0' can be used in an address specification like
1 '0,/REGEXP/' so that 'sed' will try to match REGEXP in the first
1 input line too. In other words, '0,/REGEXP/' is similar to
1 '1,/REGEXP/', except that if ADDR2 matches the very first line of
1 input the '0,/REGEXP/' form will consider it to end the range,
1 whereas the '1,/REGEXP/' form will match the beginning of its range
1 and hence make the range span up to the _second_ occurrence of the
1 regular expression.
1
1 Note that this is the only place where the '0' address makes sense;
1 there is no 0-th line and commands which are given the '0' address
1 in any other way will give an error.
1
1 The following examples demonstrate the difference between starting
1 with address 1 and 0:
1
1 $ seq 10 | sed -n '1,/[0-9]/p'
1 1
1 2
1
1 $ seq 10 | sed -n '0,/[0-9]/p'
1 1
1
1 'ADDR1,+N'
1 Matches ADDR1 and the N lines following ADDR1.
1
1 $ seq 10 | sed -n '6,+2p'
1 6
1 7
1 8
1
1 ADDR1 can be a line number or a regular expression.
1
1 'ADDR1,~N'
1 Matches ADDR1 and the lines following ADDR1 until the next line
1 whose input line number is a multiple of N. The following command
1 prints starting at line 6, until the next line which is a multiple
1 of 4 (i.e. line 8):
1
1 $ seq 10 | sed -n '6,~4p'
1 6
1 7
1 8
1
1 ADDR1 can be a line number or a regular expression.
1