How can I copy only odd numbered lines from one file to another?

I have a text file with some information. How can I write only odd numbered lines from that file to a new file? Using shell script..


Solution 1:

You could use sed:

sed '2~2d' file > new-file

This starts from the second line, matches every 2nd line after that, and deletes the matched lines from the stream. The remaining odd-numbered lines are redirected to a new file.

Solution 2:

awk 'NR%2' file > newFile

NR is the number of the current line; NR%2 = odd line