neversaint has asked for the wisdom of the Perl Monks concerning the following question:

Dear Masters,
Suppose I have all the files in this directory:
[neversaint@bluemoon temp]$ ls debug.pl sometext.txt script1.pl test1.fasta
contain "foo" in it. As you can see, these files may contain a free-text or program code.

I want to replace all the "foo" with "bar" in those files, in place. Meaning that I don't want to have a new copy of a those files after replacement. Is there a Perl one-liner to do that?

---
neversaint and everlastingly indebted.......

Replies are listed 'Best First'.
Re: One-Liner for In Place Text Replacement of Multiple Files
by shmem (Chancellor) on Apr 06, 2007 at 15:32 UTC
    Sure.
    perl -pi -e 's/foo/bar' *

    To keep "fool" and "barefoot"

    perl -pi -e 's/\bfoo\b/bar/' *

    --shmem

    _($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                                  /\_¯/(q    /
    ----------------------------  \__(m.====·.(_("always off the crowd"))."·
    ");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}
Re: One-Liner for In Place Replacement of Multiple Files
by kyle (Abbot) on Apr 06, 2007 at 15:30 UTC
Re: One-Liner for In Place Text Replacement of Multiple Files
by swares (Monk) on Apr 06, 2007 at 21:34 UTC
    If you also need a backup of the modified file try...
    perl -p -i.bak -e 's/oldstring/newstring/g' *