my @filelines = ; #Read the entire file into an array my @whichline; #Declare a variable to hold the line number for(my $i = 0; $i = $#filelines; $i++){ #Go through the entire array untill you find it or run out of lines if($filelines[$i] =~ m/TEXTTOMATCH/o){ #Attempt to match, use compile-once push(@whichline,$i); #If match, add to the list of current matches } } #Now you know what lines had TEXTTOMATCH in them