Your post as been already discussed at length, I'd just like to add that (maybe you didn't know and may be glad to know that) instead of
open(FH, "wrongs") or die $!; open(FH2, ">wrongs2") or die $!; while ($line = <FH>) { $line =~ s/wrongs/wrongs3/; print FH2 "$line"; } close (FH); close (FH2);
you can use some cmd line swithces and arguments and shell redirection (this particular one works in any common shell that I know of, including command.com):
perl -lpe 's/wrongs/wrongs3/' wrongs >wrongs2
or
perl -lpi -e 's/wrongs/wrongs3/' wrongs
if you want in place editing. But under Windows AFAIK you will have to do (something like):
perl -lpe "s/wrongs/wrongs3/" wrongs >wrongs2
and
perl -lpi.bak -e "s/wrongs/wrongs3/" wrongs
respectively instead; i.e. use double quotes for quoting and cannot do in place editing withou backup.
Modified my Perl code to count the number of replacements as well as added benchmarking:
The code above can easily be adapted to show the count:
perl -lpi '$c++ if s/wrongs/wrongs3/; END{warn "count=$c\n"}' wrongs
For the benchmark, it's not just that easy. That's what Benchmark.pm is for, and indeed it works by repeating the code to be tested a suitable number of times. But if the task takes long enough, then bash's time command will suffice: here's a test on a file that's 3494270 lines long:
$ time perl -lpe '$c++ if s/sex/cool/; END{warn "count=$c\n"}' gse1.lo +g >/dev/null count=2840 real 0m19.062s user 0m16.736s sys 0m0.940s
Unfortunately it's not available under command.com and cmd.exe, that I know.

Last, you may also want to count the number of substitutions when the /g global modifier is given. In that case

perl -lpi '$c+=s/wrongs/wrongs3/g; END{warn "count=$c\n"}' wrongs
or else you may use the "highly experimental" </c>(?{ code })</c> regex feature:
perl -lpi 's/wrongs($c++)/wrongs3/g; END{warn "count=$c\n"}' wrongs
but there's really no need for it here...

In reply to Re: Word replace - notetab light vs perl by blazar
in thread Word replace - notetab light vs perl by kiat

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.