Here are just the simplest ways to do this.
SED implementation
One:
Odd line sed-n ' p;n ' file
Even line sed-n ' n;p ' file
This may be difficult to understand, as an example on the Internet:
example, remove even rows from the AAA file
Cat AAA
This is 1
This is 2
This is 3
This is 4
This is 5
Sed-n ' n;p ' AAA//-n means cancel default output
This is 2
This is 4
Note: Sed reads this is 1, executes the n command, at this time the mode space is the This is 2, the execution p, print mode space content This is 2, then read this is 3, execute the n command, at this time the mode space is the is 4, execute p, print mode space content this is 4, after reading this is 5, executes the n command because there is no, so quit, and discard the P command.
So, the final print is even lines.
Two:
Odd line sed-n ' 1~2p ' file
Even line sed-n ' 2~2p ' file
awk implementation
One:
Odd line awk ' nr%2==1 ' file
Even line awk ' nr%2==0 ' file
Two:
Odd line awk ' nr%2 ' file
Even line awk '! (nr%2) ' File
This article is from the "Progress a little every day" blog, be sure to keep this source http://yujia2016.blog.51cto.com/59379/1829449
Print odd or even rows with awk and SED