I would localise the $/ variable and set it to the string that splits your log entries "*****\n*****". If you then read from the file in a scalar context it will give you one complete log entry.

You then need to remove the string you split the file on ("*****\n*****") and split the remainder on a new line "\n". Place the result of this split in an array. If the last element of this array starts with "Server Closed", then you have a complete log entry.

#!/usr/bin/perl -w use strict; my @status; { my ($log, @split_log, $status); local $/ = qq{*****\n*****}; while ($log = <DATA>) { $log =~ s/\Q*****\E\n\Q*****\E$//; @split_log = split "\n", $log; $status = ($split_log[$#split_log] =~ m/^Server Closed/) ? "complete\n" : "incomplete\n"; push @status, $status; } } __DATA__ ***** Server Started Monday data1 data2 Server Closed Tuesday ***** ***** Server Started Wednesday data3 data4 Server Closed Friday ***** ***** Server Started data5

This leaves the string "*****" on the front of the first log entry. You can then do whatever processing you need on the logs either one complete log at a time, or instead of assigning the split to @split_log, create an anonymous array and push that onto @split_log. This will let you defer your processing to the end.

Nuance


In reply to RE: Multiple session log extraction from a single file problem by nuance
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.