Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re: Search and replace within a file

by petdance (Parson)
on May 08, 2012 at 18:30 UTC ( #969515=note: print w/ replies, xml ) Need Help??


in reply to Search and replace within a file

Take a look at the -i switch, which does in-place file transformations.

Examples are in my presentation A Field Guide To The Perl Command Line.

xoxo,
Andy


Comment on Re: Search and replace within a file
Re^2: Search and replace within a file
by sriharsha.sm (Initiate) on May 10, 2012 at 13:55 UTC
    I thought of recommending the inplace replace using sed. Sorry for recommending sed over perl here.. But I believe this needs to be done in sed than in perl.
    $sed -i 's#uname#newname#' /etc/hosts/*
    I did some time comparison for both perl and sed and it turns out that sed has better performance
    $time sed -i 's/sri/harry/' * sed -i 's/sri/harry/' * 0.00s user 0.01s system 37% cpu 0.016 total $time perl -pi -e 's/harry/sri/' perl -pi -e 's/harry/sri/' * 0.01s user 0.01s system 62% cpu 0.029 to +tal
      But I believe this needs to be done in sed than in perl. [...] I did some time comparison for both perl and sed and it turns out that sed has better performance
      $time sed -i 's/sri/harry/' * sed -i 's/sri/harry/' * 0.00s user 0.01s system 37% cpu 0.016 total $time perl -pi -e 's/harry/sri/' perl -pi -e 's/harry/sri/' * 0.01s user 0.01s system 62% cpu 0.029 to +tal

      So, you suggest to optimize a one-shot command run every few day, weeks, or months for a total execution speed advantage of 10 milliseconds on your machine, based on one single run of each command? Are you serious?

      (If yes, let me suggest to use a one-letter shell alias for "perl -pi -e". This saves you 10 keystrokes and thus gains you much more than 10 milliseconds, even if you can type really fast.)

      Alexander

      --
      Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://969515]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (6)
As of 2014-12-20 12:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (95 votes), past polls