How to split a file in two on even/odd line numbers

I have a (CSV) file that I want to split in two. I want all the even lines to go to one file, and all the odd lines to go to another file.

$ ls -1
FILE
[MAGIC]
$ ls -1
FILE
ALL_THE_EVEN_LINES
ALL_THE_ODD_LINES

This is how to do it in two passes using awk:

awk 'NR%2==0' FILE > ALL_THE_EVEN_LINES
awk 'NR%2==1' FILE > ALL_THE_ODD_LINES

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.