neversaint neversaint - 2 months ago 7
Linux Question

How to remove odd lines except for first line using SED or AWK

I have the following file

# header1 header2
zzzz yyyy
1
kkkkk wwww
2


What I want to do is to remove odd lines except the header
yielding:

# header1 header2
zzzz yyyy
kkkkk wwww


I tried this but it removes the header too

awk 'NR%2==0'


What's the right way to do it?

Answer

Works on GNU sed

sed '3~2d' ip.txt 

This deletes line numbers starting from 3rd line and then +2,+4,+6, etc

Example:

$ seq 10 | sed '3~2d'
1
2
4
6
8
10