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

Hi Monks.

After find the following node, One-Liner for In Place Text Replacement of Multiple Files, I was wondering if there was a solution for Windows users? For example,

perl -pi.bak -e "s/cheese/soup/g" *.txt


...was unsuccessful so I tried the following:

perl -pi.bak -e "for(<*.txt>) {s/cheese/soup/g}"


...but Perl displays the following response:

-i used with no filenames on the command line, reading from STDIN.

Assuming this is possible on a Windows machine, any help would be appreciated.

Thanks.

Sarah.

Replies are listed 'Best First'.
Re: One-liner search and replace with multiple files...on Windows.
by choroba (Cardinal) on Dec 03, 2016 at 20:44 UTC
    On Windows, *.txt isn't expanded by the shell as in Linux. But Perl can expand it itself: just use glob.
    perl -pi.bak -e "BEGIN { @ARGV = map glob, @ARGV } s/cheese/soup/g" *. +txt

    Don't use filenames containing spaces or wildcard characters with this, though.

    ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,
      On Windows, *.txt isn't expanded by the shell as in Linux. But Perl can expand it itself: just use glob.

      There is a module for that: Win32::Autoglob. With a minor patch, it would also work on the nearly-dead other Microsoft operating systems MS-DOS and OS/2 (see Re^3: Perl Rename).

      Alexander

      --
      Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)
Re: One-liner search and replace with multiple files...on Windows.
by Laurent_R (Canon) on Dec 03, 2016 at 21:48 UTC
    Or use bash under Windows.
Re: One-liner search and replace with multiple files...on Windows.
by Marshall (Canon) on Dec 05, 2016 at 00:00 UTC
    Perhaps look at Win32::Autoglob?.
    The Windows shell doesn't do what the Unix shell does in regards to globbing.