Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re: Questions about Grep

by friedo (Prior)
on May 29, 2008 at 19:02 UTC ( [id://689068]=note: print w/replies, xml ) Need Help??


in reply to Questions about Grep

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.

Replies are listed 'Best First'.
Re^2: Questions about Grep
by pc88mxer (Vicar) on May 29, 2008 at 19:14 UTC
    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>;
Re^2: Questions about Grep
by Anonymous Monk on May 29, 2008 at 19:23 UTC
    Thanks!

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (6)
As of 2024-03-28 10:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found