· Offer Kaye > Perl > Print Odd Lines
Explanation of 'Print Odd 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 <>, but since <> is not inside a while() $_ will not
be updated, and "print" will print line number 1. The next line (3) will now be read using while(<>), this will discard line 2 and
update $_ with the contents of line 3- and so on...
Thus, only the odd 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:
a
c
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