[Perl] split file into odd and even lines

Offer Kaye oferk at oren.co.il
Sun Oct 6 02:34:17 PDT 2002


Hi all,
Recently I needed to split a given file into the odd and even lines, and
send each of these parts into a different file. I got curious about what the
shortest solution in Perl would be, and the joint efforts of myself and my
friend Shlomo Yona led us to this solution:

(perl -ne'if($.%2){print}else{warn $_}' a>b)>&c

Note that this will not work under Bash, because of the use of shell
redirection at the end.

My question is- can anyone improve on this solution? I.e., given an input
file "a" and output files "b" and "c", can you split "a" into the odd and
even lines into files "b" and "c", in less characters than the above
solution?

Good luck :-)
Offer Kaye




More information about the Perl mailing list