[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Omaha.pm] Parsing a Text File



On Fri, 29 May 2009, Andrew Embury wrote:

Can anyone point me in the right direction on a good way to regex match a
file and only output the lines that contain one of the patterns in the
array?  The text file is large, so iterating the file mutiple times is not a
good solution for me.

If I understand your question correctly, you might try using
Regexp::Assemble.   I.e., something like:

use Regexp::Assemble;

my $ra = Regexp::Assemble->new();
foreach (@regexps) {
  $ra->add($_);
}
my $re = $ra->re;

open(my $FH, '<', "/path/to/file")
  or die "Couldn't read from /path/to/file: $!";
while (<$FH>) {
  /$re/ and print;
}
close($FH);

Untested, YMMV, use only as directed, etc. :)

Chris St. Pierre
Unix Systems Administrator
Nebraska Wesleyan University