Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
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":



  • 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 cooling their heels in the Monastery: (5)
As of 2022-05-19 09:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you prefer to work remotely?



    Results (71 votes). Check out past polls.

    Notices?