Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Perl as grep

by ibanix (Hermit)
on Dec 02, 2003 at 15:05 UTC ( [id://311637]=perlquestion: print w/replies, xml ) Need Help??

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

Hi folks,

I'm on a Windows server with ActivePerl, but no grep. How can I make perl work like grep -- with the command line?

I tried perl -p "*.*" -e "print $_ if /regex/" and it's complaining about the *.*.

The line interface for perl isn't my forte, can anyone offer me help?

Thanks!
ibanix

$ echo '$0 & $0 &' > foo; chmod a+x foo; foo;

Replies are listed 'Best First'.
Re: Perl as grep
by duff (Parson) on Dec 02, 2003 at 15:19 UTC
    I tried perl -p "*.*" -e "print $_ if /regex/" and it's complaining about the *.*.

    Aye, that's because you've got it in the wrong place. It looks like you want:

    perl -ne "print if /regex/" *.*

    or maybe

    perl -pe "next unless /regex/" *.*

    Though I'm not sure about the globbing under windows. I seem to recall that each program must do its own. Perhaps what you really need is this:

    perl -ne "BEGIN { @ARGV = glob(qq(@ARGV)); } print if /regex/" "*.*"

    caveat lector! this code is untested.

      duff++

      Note that one of your examples:

      perl -pe "next unless /regex/" *.*
      doesn't do what you think. -p puts the print statement in the while(<>) loop's continue block and so it will get called when you call next. So that one-liner prints all lines of a file named "*.*" (yes, you are correct about globbing, much to my consternation -- the reasons for this are nonsense), just not as efficiently because it applies a regex to each line and then effectively ignores the result of that test.

                      - tye
Re: Perl as grep
by gjb (Vicar) on Dec 02, 2003 at 15:22 UTC

    The -p argument doesn't take an argument and prints any line read. What you want is:

    perl -n -e "print if /regex/" *.*

    Hope this helps, -gjb-

Re: Perl as grep
by shotgunefx (Parson) on Dec 02, 2003 at 16:23 UTC
    You can check out the Perl Power Tools project. A perl grep is available here.


    -Lee

    "To be civilized is to deny one's nature."
Re: Perl as grep
by Abigail-II (Bishop) on Dec 02, 2003 at 16:20 UTC
    Is making perl act as grep really the solution for your problem? Today you want to Unix tool grep on Windows, tomorrow something else.

    Why not install one of the Unix tool packages for Windows, like cygwin?

    Abigail

      You assume I am allowed to install cygwin on it :-)

      $ echo '$0 & $0 &' > foo; chmod a+x foo; foo;
        Non Perl Answer for WinNT/2000/XP in CMD.EXE is  findstr dose some of grep's functions and it does (an MS) regex search. MADuran
Re: Perl as grep
by DrHyde (Prior) on Dec 02, 2003 at 15:23 UTC
    The solution is on your friendly local CPAN mirror: here
Re: Perl as grep
by cored (Scribe) on Dec 02, 2003 at 15:47 UTC
    Maybe this can help you
    perl -e '/regex/&&print'

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://311637]
Approved by coreolyn
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (2)
As of 2024-04-25 06:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found