Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Questions about Grep

by Anonymous Monk
on May 29, 2008 at 18:59 UTC ( [id://689067]=perlquestion: print w/replies, xml ) Need Help??

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

HI!

Sorry for this really newbie question, but I was just wondering if grep works the same in UNIX and perl.

Right now I can do this in grep:

grep pattern filename

And i'll know if that pattern occurs in the file. But from my reading so far, as I can tell, this function in perl only works in the sense that grep(pattern,list) but I would rather search a file then a list.

I have alternative ways to perform the action of grep. But I'd like to use the power of grep to do my dirty work for me.

Thanks!

Replies are listed 'Best First'.
Re: Questions about Grep
by friedo (Prior) on May 29, 2008 at 19:02 UTC
    If you want to grep a file in Perl, you can read it into an array first:
    use strict; open my $fh, "file.txt" or die "Error opening file.txt: $!"; my @lines = <$fh>; my @matches = grep /pattern/, @lines;
    There's also the File::Grep module from CPAN which provides some easy-to-use file grepping functionality.
      Don't know if this is any more efficient, but it's a few less keystrokes:
      use strict; open my $fh, "file.txt" or die "Error opening file.txt: $!"; print grep /pattern/, <$fh>;
      Thanks!
Re: Questions about Grep
by dwm042 (Priest) on May 29, 2008 at 20:06 UTC
    As others have pointed out, grep in Perl and grep in various flavors of Unix are not the same. Further, there can be interesting variations in the kinds of patterns that different versions of (Unix) grep can handle (grep, egrep, fgrep, etc).

    Certain versions of Red Hat had a grep that accepted the -P flag, which then would allow the pattern to be a Perl style regular expression. I personally found it useful to be able to say things like:

    grep -P "(199|200)\d\w+" my.mail.log
      (shameless plug:) I seriously needed a command-line grep tool that could do real perl regexes, so I wrote one: grepp -- Perl version of grep -- it even handles encoding conversions and unicode expressions. (I know others have done similar exercises, but it's hard to resist doing it yourself...)
Re: Questions about Grep
by oko1 (Deacon) on May 29, 2008 at 20:28 UTC

    If you take a look at 'man grep' and 'perldoc -f grep', you'll note a few differences. E.g. - commandline grep searches files, while Perl's grep searches lists. Commandline grep takes options; Perl's grep takes an expression or a subroutine. Commandline grep is limited to matching regular expressions; Perl's grep will return the matching elements for any test that returns 'true' for those elements.

    In short, the very basic premise of "global regular expression print" - matching and returning a specified item - is about the only thing that the two have in common, and you're much safer assuming that what you know about one does not apply to the other.

    
    -- 
    Human history becomes more and more a race between education and catastrophe. -- HG Wells
    
Re: Questions about Grep
by planetscape (Chancellor) on May 29, 2008 at 21:45 UTC

    For grep versions and related alternatives, please see grep , etc.

    HTH,

    planetscape

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (7)
As of 2024-04-25 15:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found