dlcasey has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I posted something similar last week but didn't properly explain what I needed to do and
I still can not figure it out. I have an application log I'm trying to retrieve a specific sequence of 4 lines
from(event1,event2,event3, event4). I've successfully filtered the log to get all occurances the four lines
but now only want to pull out the blocks of lines where the events occur in sequence.
EX:

09:12:50:861 EVENT1 #discard
09:13:09:467 EVENT2 #discard
09:13:09:837 EVENT1 #keep
09:13:38:059 EVENT2 #keep
09:14:03:115 EVENT3 #keep
09:14:04:076 EVENT4 #keep
09:14:11:376 EVENT1 #discard
09:14:34:049 EVENT2 #discard
09:14:34:990 EVENT3 #discard
09:14:34:990 EVENT3 #discard
09:14:34:990 EVENT4 #discard
09:15:09:837 EVENT1 #keep
09:16:38:059 EVENT2 #keep
09:17:03:115 EVENT3 #keep
09:18:04:076 EVENT4 #keep

I want to extract the data when the four events happen in an unbroken sequence and print them to a file
Can anyone help me? I'm fairly new to Perl and development in general.
Thanks again!

Replies are listed 'Best First'.
Re: Log Parsing Help
by ikegami (Patriarch) on Sep 08, 2009 at 17:31 UTC
    Doesn't the solution I provided to your last question do exactly that?

    Update: Actually, no. You need to remove the three lines mentioning $last:

    my @history; my $expect = 1; while (<>) { chomp; my ($num) = /EVENT(\d+)/ or next; # Ignore bad input. if ($num == 1 || $num != $expect) { @history = (); $expect = 1; } if ($num == $expect) { push @history, "$_\n"; if ($expect++ == 4) { print(@history); @history = (); $expect = 1; } } }

    The funny thing is that I didn't have those three lines originally since I had forgotten that part of your question when I first wrote up my reply.

      Many, many thanks! That did it.
Re: Log Parsing Help
by toolic (Bishop) on Sep 08, 2009 at 17:27 UTC
    This question is identical to the one you asked in Parsing Pattern Question. Please read through the responses you received there, and post any new information under that thread.
      Only because he edited his old post. It used to be a different question.
Re: Log Parsing Help
by SuicideJunkie (Vicar) on Sep 08, 2009 at 17:33 UTC

    What algorithms have you tried?

    As I understand it, all you want to do is:

    • If the line is NOT event #{counter} set counter to undef/zero
    • If the line is event #1, set counter to 1
    • If the line is event #{counter}, increment counter
    • If the counter is now greater than {num events: IE 4}, save the last four lines off to somewhere and reset the counter to undef/zero

    - SJ

    The monks can fry your fish, and they can give you some tips and some bait, but you still need to wake up in the morning and climb onto the boat.