use strict; use warnings; my ($input_file) = @ARGV; # Auto-generate output names based on input name my @output_file = map { "$input_file.$_" } 1..2; # Open up all the required filehandles open($input_fh, "<", $input_file) || die "Could not open $input_file\n"; my @out_fh; open ($out_fh[0], ">", $output_file[0]) || die "Could not write to $output_file[0]\n"; open ($out_fh[1], ">", $output_file[1]) || die "Could not write to $output_file[1]\n"; # Start by writing to the first filehandle my $fh = $out_fh[0]; while (<$input_fh>) { $fh = $out_fh[1] if (/^DETS01/); print $fh; } # Now close out everything foreach my $out_fh (@out_fh) { close($out_fh); } close($input_fh);