in reply to How to scan a file, find a character string and print from that string to EOF
This rotates filehandles if the string "DETS01" occurs. The first bit is in the first file, the second in the second. So the file "input.txt" makes "input.txt.1" and "input.txt.2".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);
|
|---|