my $found_first = 0; my $found_second = 0; while () { if (/002389983/) { print "Looking for: 002389983\n Found it in file: $_\n"; $found_first = 1; last; } } if ($found_first) { seek LOG, 0, 0; while () { if (/002389983/) { print "Looking for: 29994339499\n Found it in file: $_\n"; $found_second = 1; last; } } } unless ($found_second) { print "Could not find the second string: 29994339499\n"; }