This question already has an answer here:

Suppose I've a file named "a.txt" and it has 100 lines. Now I want to extract odd lines i.e lines 1,3,5...etc and save them to a different file such as Odd.txt and the same thing for all even lines to save them in the even.txt file keeping the source file as it is.

marked as duplicate by don_crissti, Stephen Kitt, Jeff Schaller, PersianGulf, user34720 Jun 6 '16 at 18:13

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.


You can use awk:

awk '{ print > (NR % 2 ? "odd.txt" : "even.txt") }' a.txt

This reads from a.txt and appends lines to either odd.txt or even.txt depending on the current line number.


With sed:

sed -ne 'w odd.txt' -e 'n; w even.txt' a.txt

Not the answer you're looking for? Browse other questions tagged or ask your own question.