$ perl -Mstrict -Mwarnings -le ' my @eachline = ( "transaction blah find blah blah think blah save_param", "start_sub blah url blah blah submit transaction blah find" ); my @keywords = qw{transaction find think save_param start_sub url submit}; my $pattern = "(" . join("|" => map { "\\b$_\\b" } @keywords) . ")"; my %count; for (@eachline) { ++$count{$1} while /$pattern/g; } print "No. of matches for $_ is $count{$_}" for @keywords; ' No. of matches for transaction is 2 No. of matches for find is 2 No. of matches for think is 1 No. of matches for save_param is 1 No. of matches for start_sub is 1 No. of matches for url is 1 No. of matches for submit is 1