my $line = 'This line 1 has a hit here and a hit there.'; my $word = 'hit'; my $count = 0; my $n = 0; my @stuff = split m/($word)/, $line; grep { $n++; if ($n % 2) { print $_; } else { print RED, $_, RESET; $count++; } } @stuff; print "\nFound $count times.\n";