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

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