use strict; use warnings; my $string = shift; my $file = 'electricity.txt'; open my $fh, '<', $file or die $! my $counter = 0; while (<$fh>) { if (s/\Q$string\E/($string)/g) { $counter++; print "$counter "; } print; } close $fh;