my $file = join '', @file; # put it all in a single scalar if ($file =~ m/(?:what you want to find.*){6}/) { print "'what you want to find' was in the file at least 6 times.\n"; } #### my $count; while (<$fh>) { $count++ while /what you want to look for/g; if ($count >= 6) { last; } } $fh->close(); if ($count >= 6) { print "found 'what you want to look for' at least 6 times in the file.\n"; }