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

I was trying this on a win2k machine on the command line

dir /b/S *.dsp | perl -pe"s#\bGPP\b#Arjun#i;" -i.bak

However, this operates on file names instead of working on the file content since the filter supplies the output of the previous command as STDIN to the perl command. Any ideas on how to make it operate on files?

thanks,
Saurabh

Replies are listed 'Best First'.
•Re: command lineIn place edit with a filter
by merlyn (Sage) on Apr 04, 2003 at 17:15 UTC
    Presuming you mean to take the newline-delimited names and treat them as input to the inplace-edit process, I think this will do (untested, as I'm not on windows, and I may get the quoting wrong):
    dir /b/S | perl -pe "BEGIN { chomp(@ARGV = <STDIN>) } s#\bGPP\b#Arjun# +i;" -i.bak

    -- Randal L. Schwartz, Perl hacker
    Be sure to read my standard disclaimer if this is a reply.

Re: command lineIn place edit with a filter
by broquaint (Abbot) on Apr 04, 2003 at 16:06 UTC
    Assuming you get the full file name ...
    dir /b/S *.dsp | perl -e 'BEGIN{@ARGV=map{ chomp and $_ }<ARGV>}' \ -i.bak -pe "s#\bGPP\b#Arjun#i;"
    No longer a oneliner, but it should do the job.
    HTH

    _________
    broquaint

Re: command lineIn place edit with a filter
by jasonk (Parson) on Apr 04, 2003 at 15:37 UTC
    perl -pi.bak -e"s#\bGPP\b#Arjun#i;" *.dsp
    We're not surrounded, we're in a target-rich environment!
      Have to go to n-levels in the directory. This will only work for the current directory.

      By the way, I get the message "Can't open *.dsp: Invalid argument.". Remember this is windows :-(

      Saurabh

        use G;

        Without additional options it'll glob the usual way, if you do it like this

        perl -MG=R -pi.bak -e"s#\bGPP\b#Arjun#i;" *.dsp
        it'll glob recursively.

        P.S.: You can install the module by PPM from http://jenda.krynicky.cz/perl

        Jenda
        Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live.
           -- Rick Osborne

        Edit by castaway: Closed small tag in signature

Re: command lineIn place edit with a filter
by thesundayman (Novice) on Apr 04, 2003 at 15:37 UTC
    Well I figure I have to use something like xargs but any idea on how to achieve it on windows? - since we don't have xargs :-(

    Finally, I wrote a script using File::Recurse but a one liner would be cool. Thanks in advance.

    Saurabh

      for /R %f in (*.dsp) do perl -ibak -pe "s#\bGPP\b#Arjun#i;" %f

      Not quite xargs ;-) 'for' will do much more than it used to. The '/R' makes for look recursively in each subdirectory.

Re: command lineIn place edit with a filter
by PodMaster (Abbot) on Apr 04, 2003 at 16:29 UTC
    try `find2perl` and use File::Find if you want it to go N directories (cause DIR don't do that), otherwise try `perldoc -f glob'.
    perl -le"BEGIN{@ARGV=glob shift;}print for @ARGV;" *.dsp perl -i.bak -pe"BEGIN{@ARGV=glob shift;}s#\bGPP\b#Arjun#i" *.dsp
    If the above is unclear, then `perldoc -f shift' ;)


    MJD says you can't just make shit up and expect the computer to know what you mean, retardo!
    I run a Win32 PPM repository for perl 5.6x+5.8x. I take requests.
    ** The Third rule of perl club is a statement of fact: pod is sexy.

Re: command lineIn place edit with a filter
by graff (Chancellor) on Apr 05, 2003 at 02:35 UTC
    I actually wrote a perl utility called "resub" (and posted it here that could take a list of file names on stdin, take one or more perl regex substitutions as command line args (or from files named with command line args), and apply the substitutions globally in each file (optionally appending some suffix to each file name when saving new version of each file). Hope that helps.

    As for "dir /b/S", personally I prefer to use unix utils that have been ported to windows (e.g. by Cygwin, Delorian or GNU) such as "find", "ls", "bash", etc.