Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

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



  • 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 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? | Other CB clients
Other Users?
Others studying the Monastery: (3)
As of 2021-08-03 18:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My primary motivation for participating at PerlMonks is: (Choices in context)








    Results (39 votes). Check out past polls.

    Notices?