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=");'
-
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.