[Perl] split file into odd and even lines

Mikhael Goikhman migo at homemail.com
Sun Oct 6 04:50:39 PDT 2002

On 06 Oct 2002 11:34:17 +0200, Offer Kaye wrote:
> 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?

  perl -pe'<>'<a>b
  perl -pe'$_=<>'<a>c


More information about the Perl mailing list