Print odd-numbered lines, print even-numbered lines
I prefer to be POSIX compatible, whenever possible, so I thought I'd post this alternative method. I often use these to mangle text, before xargs
pipelines.
Print Even Numbered Lines,
sed -n 'n;p'
Print Odd Numbered Lines,
sed -n 'p;n'
Although I often use awk
, it's overkill for this type of task.
That's easy:
sed -n 2~2p filename
will print even-numbered lines from filename
sed -n 1~2p filename
will print odd-numbered lines.
For even numbers the code should be
awk 'NR%2==0' filename
& for odd numbers
awk 'NR%2==1' filename