use strict; use warnings; open(FILE,"Daniel.txt") or die "Unable to open file 'Daniel.txt': $!\n"; open(my $steffi,">Steffi.txt") or die "Unable to open file 'Steffi.txt': $!\n"; my $fh = "none"; while () { chomp; if (/^\s*Daniel\s+Lastname\s*$/s) {$fh = $daniel;} if (/^\s*Steffi\s+Lastname\s*$/s) {$fh = $steffi;} if ($fh ne "none") {print $fh $_;} } close($steffi); close($daniel); close(FILE);