http://qs321.pair.com?node_id=11114190


in reply to Print multiple lines based on condition

What came to my mind was to use Tie::File, which lets you "access the lines of a disk file via a Perl array" without reading the whole file into memory. The code below is incomplete and untested, but it shows the logic of how I would approach this using Tie::File.

use strict; use warnings; use feature 'say'; use Tie::File; my $input_file = shift; my $output_file1 = shift; my $output_file2 = shift; my @lines; tie @lines, 'Tie::File', $input_file or die "Unable to open file '$inp +ut_file': $!"; my $index = 0; open(my $fh1,">",$output_file1) or die "Unable to open file '$output_f +ile1': $!"; open(my $fh2,">",$output_file1) or die "Unable to open file '$output_f +ile1': $!"; while ($index <= $#lines) { next if (); #insert logic to determine which lines are ignored my $current_line = $lines[$index]; my $fh; if () { #insert logic to determine that current line goes to firs +t file $fh = $fh1; } else { #assumes if not skipped and not for first file, goes to 2n +d file $fh = $fh2; } say $fh $lines[$index]; #if needed, modify next if statement to determine if current line +requires #copying the next 4 input file lines if ($lines[$index] =~ m/SDP_CMD_WRSIZEDFULL/i) { say $fh $lines[$index + 1]; say $fh $lines[$index + 2]; say $fh $lines[$index + 3]; say $fh $lines[$index + 4]; $index += 5; next; } $index++; } untie @lines; close($fh1); close($fh2);