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

Hi I need to parse a logfile in which i should do multiline grepping. Logfile: This logfile contains logs of multiple lines.In this file i need to grep three patterns. Eg logfile: Tm500 is tried to execute an attach script.
rrcConnectionRelease{ attach request measurementReport{ Event A1 Id 13 Purpose . . . . . . .so on lines
my $logpattern1=rrcConnection; my $logpattern2=measurementReport; my $logpattern3=Event; open(LOGFILE,"$filepath"); my @file=<FILE>; my $f=join("",@file); if ($f=~/.*$logpattern1.*?$logpattern2.*?$logpattern3/gs){ &statuslog("Found log marker : $logpattern1 $logpattern2 $logpattern3 + in TM500log\n"); } else{ &errorlog("Failed to find log marker: $logpattern2 $logpattern3 in $lo +gpattern1 in TM500log\n"); } close(LOGFILE);
I had tried this code, its working. But i need a generic way where in i can grep for more than three log patterns. Is it possible to grep these patterns by storing into an array variable? Any help would be appreciated. Thanks in advance

Replies are listed 'Best First'.
Re: Generic multiline grepping
by Happy-the-monk (Canon) on Mar 05, 2015 at 08:11 UTC

    my @file=<FILE>;

    You have errors in your code preventing success:

    First, quote your strings:

    my $logpattern1='rrcConnection'; my $logpattern2='measurementReport'; my $logpattern3='Event';

    Second, remember the name of your open filehandle:

    open(LOGFILE,"$filepath"); my @file=<LOGFILE>;

    #.........^^^

    Cheers, Sören

    Créateur des bugs mobiles - let loose once, run everywhere.
    (hooked on the Perl Programming language)

Re: Generic multiline grepping
by vinoth.ree (Monsignor) on Mar 05, 2015 at 07:34 UTC

    If you can use Perl version 5.10, then there is a really easy way to do that. Just use the new smart match (~~) operator.

    use warnings; use strict; use 5.10.1; my @matches = ( qr/pattern1/, qr/pattern2/, ); if( $_ ~~ @matches ){ ... }

    Not tested

    Update:

    Tested Now,Make changes in your pattern as needed.

    use strict; use warnings; use 5.10.1; my $logpattern1='rrcConnection'; my $logpattern2='measurementReport'; my $logpattern3='Event'; open(LOGFILE,"./log.txt") or die "$!"; my @file=<LOGFILE>; my $f=join("",@file); my @matches = ( qr/.*$logpattern1/, qr/.*$logpattern2/, qr/.*$logpattern3/, ); if( $f ~~ @matches ){ print("Found log marker : $logpattern1 $logpattern2 $logpattern3 in T +M500log\n"); } else{ print("Failed to find log marker: $logpattern2 $logpattern3 in $logpat +tern1 in TM500log\n"); } close(LOGFILE);

    All is well. I learn by answering your questions...
      Hi Vineeth, Just now i tested. But here, initially pattern1 should be present and under pattern1 pattern2 should be present and pattern3 should be present under pattern3. whatever the code i had posted previously, its working. but the problem is, that code will work only for 3 nested pattern matching. If you had any idea please suggest me how i can do that nested pattern matching for more than 3 patterns which should be in a generic way. Thanks in advance
      Thank you vinoth.
Re: Generic multiline grepping
by hdb (Monsignor) on Mar 05, 2015 at 09:05 UTC

    Under the assumption that your log file has a hierarchical structure as indicated by the opening braces (there are corresponding closing ones as well?) I find the matching approach dangerous. You can easily match across several hierarchies of braces opening and match across different log file entries.

    Instead you should parse the log file into a hierarchical structure and then evaluate the entries. Or as a minimum include or forbid opening and closing braces in your regex.