cmarcum has asked for the wisdom of the Perl Monks concerning the following question:

All knowing Monks, Why does
perl -pi -w -e 's/orgtext/$./g;' *.html

result in a repetition of the first index of $. ?

For example, the output looks like this for an html file with three lines each containing the string 'orgtext':
1
1
2


Why does it not print this:
1
2
3

?

Replies are listed 'Best First'.
Re: command line replace incorrectly indexes $.
by ikegami (Patriarch) on Oct 17, 2007 at 22:54 UTC

    I cannot reproduce your results on ActivePerl for Windows. Are you sure each "orgtext" is on a different line? $. is the current input line number, not the number of substitutions performed.

    a.htmlb.html
    Before
    orgtext orgtext orgtext orgtext
    orgtext orgtext orgtext orgtext
    After
    1 1 2 3
    4 4 5 6

    Perhaps you want

    perl -pi -e 's/orgtext/++$x/eg;' *.html

    a.htmlb.html
    After
    1 2 3 4
    5 6 7 8

    or

    perl -pi -e 's/orgtext/++$x/eg; $x=0 if eof(ARGV)' *.html

    a.htmlb.html
    After
    1 2 3 4
    1 2 3 4

    Update: Compacted node by replacing lists with tables.

      Hmm. I am running perl, v5.8.8 built for i486-linux-gnu-thread-multi.

      The result I want is:

      a.htmlb.html
      Before
      orgtext orgtext
      orgtext
      orgtext
      
      
      orgtext 
      orgtext
      orgtext
      orgtext
      
      After
      1 
      2
      3
      4
      
      1
      2
      3
      4
      



      But the result I get is:

      a.htmlb.html
      After
      1 
      1
      2
      3
      
      1
      1
      2
      3
      

        I've already explained why your code produces the a.html you're seeing.

        Your code should not produce the b.html you are seeing unless you are wrong about your input, you are wrong about your code, you are wrong about your output, or your Perl is broken.

        I've already shown the code that will get you what you want, so what you are asking now?

Re: command line replace incorrectly indexes $.
by FunkyMonk (Bishop) on Oct 17, 2007 at 22:49 UTC
    It seems to me that there's a lot you're not telling us. You'd get better help if you posted your input file (trimmed down, if necessary).