in reply to Parsing Pattern Question
| Parent post as it stood when I replied |
|---|
|
H,
Perl newb here....
I have an application log I'm trying to retrieve a specific sequence of 4 lines from(event1, event2,event3, event4 - discarding any duplicates in between). I've already filtered the log to get all occurances the four lines I want but I don't know how to pull out the four lines when they happen sequentially. EX: 09:12:50:861 EVENT1 #Don't want this line 09:13:09:467 EVENT1 #Don't want this line 09:13:09:837 EVENT1 09:13:38:059 EVENT2 09:14:03:115 EVENT3 09:14:04:076 EVENT4 09:14:11:376 EVENT1 09:14:34:049 EVENT2 09:14:34:990 EVENT3 09:14:34:990 EVENT3 #Don't want this line 09:14:34:990 EVENT4 I then need to do a time calculation between each of the four events (I can do this part....I just can't figure out how to pull out the four lines I want everytime they appear in the sequential order I am looking for) Can anyone help? Thanks! |
my $last; my @history; my $expect = 1; while (<>) { chomp; my ($num) = /EVENT(\d+)/ or next; # Ignore bad input. next if defined($last) && $last eq $_; $last = $_; if ($num == 1 || $num != $expect) { @history = (); $expect = 1; } if ($num == $expect) { push @history, "$_\n"; if ($expect++ == 4) { print(@history); @history = (); $expect = 1; } } }
Update: Handle duplicates as requested.
|
|---|