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

In reply to Re: Search and replace in all odd lines by RMGir
in thread Search and replace in all odd lines by greatshots

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.