· Offer Kaye > Perl > Print Even Lines

Explanation of 'Print Even Lines' Perl oneliner

Here is the code again: perl -pe'$_=<>'
"-p" - takes care of iterating over the input lines and also doing a print of $_ after the code supplied after "-e" is run. The oneliner can therefore be written as:
perl -e'while(<>){$_=<>;print}'
This reads as follows: read in line 1 from the input using while(<>). Since the diamond operator is inside a while() statement it will update the $_ variable. Now read in the next line (number 2) using $_=<>, thus updating $_ with line 2 and discarding line 1. "print" will print $_, which is now line number 2. The next line (3) will now be read using while(<>), this will be discarded since $_=<> will read in line 4- and so on...
Thus, only the even lines will be printed.

Usage Example

Given an input file called "example" with the contents:
a
b
c
d
Then running the following from the command line:
perl -pe'$_=<>' example
will print to STDOUT the following two lines:
b
d

Offer Kaye / To contact me, email me at: offer.kaye no spam at gmail dot com / Last Modified: Thu Jul 15 23:04:09 2004

Valid XHTML 1.0! Valid CSS!