4.2 Selecting lines by numbers

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


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


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.


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, one would use 1~2 to select the odd-numbered lines and 0~2 for even-numbered lines; 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.

The following commands demonstrate the step address usage:

$ seq 10 | sed -n '0~4p'

$ seq 10 | sed -n '1~3p'