do { next LINE; print RESULTS "$currentfile"; } until eof(); #### my $matchFound = 0; while ($currentfile=) { if( ! $matchFound ){ $matchFound = 1 if $currentfile =~ /^\d{6}/ && $search_date eq substr($currentfile,52,10); } next unless $matchFound; print RESULTS $currentfile; } #### my $currentfile = do { local $/ = undef; }; my @parts = split /(^\d{6}).{46}$search_date)/m, $s, 2; print RESULTS @parts[1,2] if $parts[1];