Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?

Re: Print multiple lines based on condition

by dasgar (Priest)
on Mar 12, 2020 at 20:36 UTC ( #11114190=note: print w/replies, xml ) Need Help??

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);

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11114190]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (1)
As of 2021-09-19 02:56 GMT
Find Nodes?
    Voting Booth?

    No recent polls found