perl -nE '$count{$1}++ while /\b(the|hi|linux)\b/ig; END{say "$_ => $count{$_}" for keys %count}' filename.txt #### #!/usr/bin/perl open $fh, '<', $ARGV[0] or die $!; while( <$fh> ) { $count{$1}++ while /\b(the|hi|linux)\b/ig; } print "$_ => $count{$_}\n" for keys %count;