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

In reply to Re: Print multiple lines based on condition by dasgar
in thread Print multiple lines based on condition by syedasadali95

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.