use strict; # this will help you catch a LOT of errors my $tail = "some string"; open(INF,"data.txt") or die "Couldn't open data file: $!"; my $linenum=0; while () { print "$linenum\n"; if ($_ =~ /|$tail/) { # do stuff } $linenum++; } close(INF);