Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (2)
As of 2024-03-19 07:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found