in reply to Extract ranges of lines from a file, saving each range to a separate file
G'day perlato,
Welcome to the Monastery.
It looks like you were doing fine up to the flip-flop conditional (if (/TRANSACTION STARTED/ .. /TRANSACTION END/) {...}) and then got a bit lost.
You can do all the remaining processing within that if block. Here you'll want to do one of three things:
The coding required is very straightforward:
#!/usr/bin/env perl use strict; use warnings; use autodie; my $filename_prefix = 'pm_1155986_out_'; my $filename_suffix = '.txt'; my $filename_number = 0; my $out_fh; my ($start_re, $end_re) = (qr{TRANSACTION STARTED}, qr{TRANSACTION END +}); open my $in_fh, '<', 'pm_1155986_in.txt'; while (<$in_fh>) { if (/$start_re/ .. /$end_re/) { if (/$start_re/) { open $out_fh, '>', $filename_prefix . $filename_number++ . $filename_suff +ix; next; } if (/$end_re/) { close $out_fh; next; } print $out_fh $_; } }
[Note I've used the autodie pragma. This avoids having to hand-craft ... or die "..." messages for all the I/O operations: a tedious and error-prone activity (which Perl will do for you if you ask it nicely).]
Here's all the input and output data (within the spoiler):
— Ken
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Extract ranges of lines from a file, saving each range to a separate file
by RonW (Parson) on Feb 27, 2016 at 00:47 UTC |