Search This Blog

Tuesday, June 14, 2011

get all the odd lines in a file

sed can be used to get all the odd or even lines in a file. Consider, for example,

$cat linenums.txt
line 1
line 2
line 3
line 4
line 5
line 6
line 7
line 8
line 9
line 10

To print all the odd lines

$sed -n '1~2p' linenums.txt
line 1
line 3
line 5
line 7
line 9

$sed -n '2~2p' linenums.txt
line 2
line 4
line 6
line 8
line 10

Followers