in reply to Re: command line replace incorrectly indexes $.
in thread command line replace incorrectly indexes $.

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

  • Comment on Re^2: command line replace incorrectly indexes $.

Replies are listed 'Best First'.
Re^3: command line replace incorrectly indexes $.
by ikegami (Patriarch) on Oct 18, 2007 at 00:31 UTC

    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?

      I'm sorry. I thought I was still logged in before. There was a typo in my last response: The result I want is:

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

        There's so many inconsistencies in your posts! You showed newlines occasionally being added, the counter magically reseting for the second file, and different behaviours for the same code. The latest is a requirement you didn't even mention initially. If anything, you asked for the opposite.

        The following code replaces the string with the line number. The close resets the counter for every file on the command line.

        perl -pi -e 's/orgtext/$./eg; close(ARGV) if eof(ARGV)' *.html