Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (1)
As of 2024-04-19 00:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found