0

I'm currently learning Perl and I'm trying to figure out how to do an if string in variable { do stuff }

I've tried many different ways such as using the eq, and =~ but it returns all the keywords within keywords.txt in oppose to the specific keyword that's found in $line

Here's my script:

#!/usr/bin/perl

use strict;
use warnings;

my $keywords = 'keywords.txt';
open( my $kw, '<:encoding(UTF-8)', $keywords )
   or die "Could not open file '$keywords' $!"
   ;    # Open the file, throw an exception if the file cannot be opened.
chomp( my @keywordsarray = <$kw> )
   ;           # Remove whitespace, and read it into an array
close($kw);    # Close the file

my $syslog = 'syslog';
open( my $sl, '<:encoding(UTF-8)', $syslog )
   or die "Could not open file '$keywords' $!"
   ;           # Open the file, throw an exception if the file cannot be opened.
chomp( my @syslogarray = <$sl> ); # Remove whitespace, and read it into an array
close($sl);                       # Close the file

foreach my $line (@syslogarray) {
   foreach my $keyword (@keywordsarray) {
      if ( $keyword =~ $line ) {
         print "**" . $keyword . "**" . "\n";
      }
   }
}
0

2 Answers 2

3

You want

while (my $line = <$sl>) {
   for my $keyword (@keywordsarray) {
      if ( $line =~ /\b\Q$keyword\E\b/ ) {
         print "**$keyword** $line";
      }
   }
}

I used \b so that the line abandoned isn't considered to include the keyword band. Note that my use of \b assumes your keywords all start and end with a word character. Something else needs to be used if that that's not the case.

But that's super slow. You're compiling number_of_lines * number_of_keywords regular expressions. The following only compiles one. It also greatly reduces the number of matches performed.

my $pat = join '|', map quotemeta, @keywordsarray;
my $re = qr/\b($pat)\b/;

while (my $line = <$sl>) {
   while ($line =~ /$re/g) {
      print "**$1** $line";
   }
}

If you just need to know whether a line matched or not, then you simple need

my $pat = join '|', map quotemeta, @keywordsarray;
my $re = qr/\b(?:$pat)\b/;

while (<$sl>) {
   print if /$re/;
}
0
2

I think you mean

...
if ( $line =~ m/\Q$keyword\E/ ) { 
   ... 
}
...

this would the be a correct check to determine if the text inside of the variable $keyword occurs somewhere in $line;

The \Q and \E flags indicate that no special characters that occur within the text of $keyword should be interpreted. You can read more about Perl regular expression flags in perldoc perlre

EDIT: as @ikegami points out, without using \b to indicate a word break the pattern above can give false positives.

2
  • If you go with this approach, you should consider pre-compiling your regular expression ahead of time as @ikegami suggests below Dec 9, 2015 at 20:38
  • 1
    Note that this will consider the line "abandoned" to include the keyword "band". That's why I used \b
    – ikegami
    Dec 9, 2015 at 20:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.