Well, i'd use a classic state machine for this problem, then we can work line-by-line, without having to load the whole file into memory.

#!/usr/bin/env perl use strict; use warnings; use diagnostics; my @blocklines; my $inblock = 0; open(my $IFH, '<', 'blockextract.txt') or die($!); while((my $line = <$IFH>)) { chomp $line; if($line =~ /parameters\ after\ change/) { # Start of a block we want to read $inblock = 1; next; } # Skip handling line unless we are in a block next unless($inblock); if($line =~ /NRG\ location/) { # Block ends here # Do whatever you want to do to the block lines # stored in @blocklines. # I'm just dumping them to STDOUT print "*** START ***\n"; print join("\n", @blocklines), "\n"; print "*** END ***\n\n"; # Clean up block @blocklines = (); $inblock = 0; next; } # just some line within the interesting block # remember it for later in @blocklines push @blocklines, $line; next; } close $IFH; exit(0);

That way, we can even modify the program very slightly to make it work via pipes, working live on a stream of data generated by some other program. We just remove the open and close calls and change the while loop a bit:

while((my $line = <>)) {

Then we can use the program on an arbitrary stream of this kind of data, and it extracts each block as soon as it is pushed into the programs STDIN:

cat blockextract.txt | perl blockextract.pl

And the only thing the state machine has to hold in memory is the block it is currently working on and a single state variable...

perl -e 'use MIME::Base64; print decode_base64("4pmsIE5ldmVyIGdvbm5hIGdpdmUgeW91IHVwCiAgTmV2ZXIgZ29ubmEgbGV0IHlvdSBkb3duLi4uIOKZqwo=");'

In reply to Re: Extract Block Of Text From Log by cavac
in thread Extract Block Of Text From Log by ImJustAFriend

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.