Numeric Addresses (sed, a stream editor)
Next: Regexp Addresses, Previous: Addresses overview, Up: sed addresses [Contents][Index]
4.2 Selecting lines by numbers
Addresses in a sed script can be in any of the following forms:
numberSpecifying a line number will match only that line in the input. (Note that
sedcounts lines continuously across all input files unless-ior-soptions are specified.)$This address matches the last line of the last file of input, or the last line of each file when the
-ior-soptions are specified.first~stepThis GNU extension matches every
stepth line starting with linefirst. In particular, lines will be selected when there exists a non-negativensuch that the current line-number equalsfirst+ (n*step). Thus, one would use1~2to select the odd-numbered lines and0~2for 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 saying50.The following commands demonstrate the step address usage:
$ seq 10 | sed -n '0~4p' 4 8 $ seq 10 | sed -n '1~3p' 1 4 7 10