Link to home
Start Free TrialLog in
Avatar of farzanj
farzanjFlag for Canada

asked on

Filtering a lines from a list of keywords

I have massive log files from which I want to filter out lines containing phrases out of a list. So, I have put the words in an array and for every line, I have to go through a foreach loop.
 
Something like
my @phrases = ( 'phrase1', 'phrase2', 'phraseN');

while(<$filehandle>)
{
    foreach my $phrase (@phrases)
   {
        print if (/$phrase/);
    }
}
       
However this is very slow.  Any way I could make it fast?
SOLUTION
Avatar of ozo
ozo
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of farzanj

ASKER

Hi Ozo,  this is really great.  However, I have a confusion.  I am trying to benchmark both the methods and the results were different.  As I printed the search strings, there appears
(?-xism:
In front of my phrases -- once in each the phrase in $_  = qr/$_/ for @phrases; method and only once in the second method like
(?-xism:phrase1|phrase2|phrase3...

Please guide.
Avatar of farzanj

ASKER

Thanks for all the help.