while (my $line = ) { # Do a match against the line, grabbing the number. my ($num) = $line =~ /SEQRES\s{2,5}(\d)\s/; # This means that the line didn't meet our requirements # as defined by the regex. So, go get the next line. # (I added this ... you may not want it, depending on # what else you're doing in the loop.) next unless defined $num; # If that number is 2, leave the while loop. last if $num == 2; # Don't chomp unless there's a possibility we'll use it. chomp $line; # If the number is 1, print it to two places. if ($num == 1) { print "$line\n"; print OUTFILE "$line\n"; } }