in reply to Search and replace in all odd lines

Keep in mind, if you're using perl -ne, ARGV never gets closed, just reopened on every file on the command line, so if you're doing the one-liner across multiple files, you may not get the expected results. (See perlvar)

Here's what I mean (the first line uses bash to set up the test case...)

for i in 1 2 3 4 5; do perl -e'print "$_\n" foreach 1..$ARGV[0]' $i > +$i;done
Once you run that, you've got 5 files in the current directory, 1..5, where 1 contains 1 line, and 5 contains 5...
perl -ne'printf "File: $ARGV \$.: %2d real line: $_",$. if $. & 1' 1 2 + 3 4 5
This results in:
File: 1 $.: 1 : real line: 1 File: 2 $.: 3 : real line: 2 File: 3 $.: 5 : real line: 2 File: 4 $.: 7 : real line: 1 File: 4 $.: 9 : real line: 3 File: 5 $.: 11 : real line: 1 File: 5 $.: 13 : real line: 3 File: 5 $.: 15 : real line: 5
So in file 2 and 3, you get the even lines, because file 1 had an odd # of lines. If we had a file 6, its even lines would get printed instead of the odd ones, since the (1+2+3+4+5) is an odd #.

Fun, eh? :)


Mike