Sorry for taking so long. (Doing other things, laundry breakfast, you know how it is.) Anyways here is an incremental solution:
package Service::ParseLog; use strict; use Carp; use Symbol qw/gensym/; # Takes the log filename and opens for parsing sub new { my $class = shift; my $file = shift or croak("No filename passed"); my $obj = {}; $obj->{file} = $file; my $fh = &gensym(); open ($fh, "<$file") or confess("Cannot read $file: $!"); $obj->{fh} = $fh; $obj->{line_no} = 0; return bless $obj, $class; } # Reads the next service section. In array context it # the lines in the service. In scalar it returns whether # there is more in the file. The service is in the # last_service field. The is_parsing field indicates # whether it found the end of the last service it started. sub read_service { my $self = shift; my $fh = $self->{fh}; my $line_no = $self->{line_no}; # Find the *'s for the service if (<$fh>) { ++$line_no; if (/^\*{5}/) { $self->{is_parsing} = 1; } else { confess("No next service at $line_no in $self->{file}"); } } else { # EOF undef($self->{last_service}); return 0; } # Grab a service section and return it my @service; $self->{is_parsing} = 1; while (<$fh>) { ++$line_no; if (/^\*{5}/) { # End of service $self->{is_parsing} = 0; last; } else { push @service, $_; } } $self->{line_no} = $line_no; $self->{last_section} = \@service; return wantarray ? @service : !$self->{is_parsing}; }
(If you want, stick a 1; and the end and make it into a module.)

How would you use it? Well like this:

my $log = new Service::ParseLog("servlog.txt"); while ($log->read_service()) { my @lines = @{$log->{last_service}}; # do stuff } if ($log->{is_parsing}) { # Incomplete last service }
Note that I did a fair amount of validation. If your format is not exactly what you described you could have some problems. The reported errors should be informative enough to figure out what is wrong though, just change the tests.

And /tell tilly if you have any problems with this. :-)


In reply to Re: Multiple session log extraction from a single file problem by tilly
in thread Multiple session log extraction from a single file problem by dmtelf

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.