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

Hi all, i have a script that looks through the logs -finds a string and prints a line, what i want is to see 3lines before that and 3 lines after(or several) in order to understand what happened prior or after
#!/usr/bin/perl -w use strict; use warnings; my $m_1 = "Receive message"; @ARGV == 2 || die "usage: $0 TAB_1 TAB_5\n"; my ( $TAB_11, $TAB_35 ) = @ARGV; my @files = <./*.log>; foreach my $file (@files) { print $file . "\n"; open (my $HAN, $file) || die "Cannot open '$log' because: $!"; while ( <$HAN> ) { next unless /$TAB_11|$TAB_35/; print "Match.$&\n", $_ if /$m_1/; }}

Replies are listed 'Best First'.
Re: printing several lines around match
by roboticus (Chancellor) on Jun 19, 2012 at 11:39 UTC

    auto_w:

    Just keep track of the last few lines while you're processing, so you'll have them handy when you hit your match:

    #!/usr/bin/perl use strict; use warnings; my @last_few_lines; while (<DATA>) { push @last_few_lines, $_; shift @last_few_lines if @last_few_lines>3; + } print $_ for @last_few_lines; __DATA__ Adam Ant Bananarama Cyndi Lauper Devo Enema M Frank Zappa George Strait Hanna Banana

    ...roboticus

    When your only tool is a hammer, all problems look like your thumb.

      Very good, works fine -the only problem it doesn't print the lines before match,only after

        auto_w:

        Sure it can. When you find the match you've the previous few lines, and can print them. The technique is just to show you how to keep track of the last few lines, in case you need to save some history.

        Update: When I first read your post, I thought you wanted the code to predict the future!

        ...roboticus

        When your only tool is a hammer, all problems look like your thumb.

        Untested, but I think it should get you on the right track.

        #!/usr/bin/perl -w use strict; use warnings; my $m_1 = "Receive message"; @ARGV == 2 || die "usage: $0 TAB_1 TAB_5\n"; my ( $TAB_11, $TAB_35 ) = @ARGV; my @files = <./*.log>; # Fixed inconsistent indentation. foreach my $file (@files) { print $file . "\n"; my @last_few_lines; my $how_many_lines = 3; my $matched = 0; open (my $HAN, $file) || die "Cannot open '$log' because: $!"; while ( (not $matched) && <$HAN> ) { # As per roboticus, slightly altered though: push @last_few_lines, $_; shift @last_few_lines if @last_few_lines > $how_many_lines; if ( m/$TAB_11|$TAB_35/ && m/$m_1/) { print @last_few_lines, $&; # Note that $& will contain the matched results from the # m/$m_1/ match, in your and my designs both. $matched = 1; } if ($matched) { $matched = 0; for (1..$how_many_lines) { print <$HAN>; last if eof $HAN; } } } }
Re: printing several lines around match
by moritz (Cardinal) on Jun 19, 2012 at 11:34 UTC
      i work on Cygwin - loading modules is a problem for me, with grep(grep -airC 5) i know how to do it but not inside of a script thou....

        auto_w:

        What problem do you have with loading modules in Cygwin? I use it all the time at work, and rarely have trouble installing modules.

        ...roboticus

        When your only tool is a hammer, all problems look like your thumb.

Re: printing several lines around match
by cheekuperl (Monk) on Jun 20, 2012 at 05:42 UTC
    There must be some CPAN module available for this, but just becuase it seemed interesing, I implemented the following:
    #!/usr/bin/perl $howmany=3; # 3 lines before match, 3 lines after match $match="match"; # What to search i.e. grep string @buff=(); # Array that contains (($howmany*2)+1) elements max at any g +iven time $cLines=(($howmany*2)+1); #Read first (($howmany*2)+1) lines into @buff $count=0; while(defined($line=<DATA>) && $count<$cLines) { chomp($line); push(@buff,$line); $count++; } #start from @buff[0], go until index ($howmany) for($idx=0;$idx<=$howmany and $idx <scalar @buff;$idx++) { if($buff[$idx]=~/$match/) { print "\n". "-" x 20;#Separator #Print $howmany lines before match for($jdx=0;$jdx<$idx and $jdx<scalar(@buff);$jdx++) { print "\n$buff[$jdx]"; } print "\n $buff[$jdx] <---- match"; # print line that matches #Print $howmany lines after match for($jdx=$idx+1;$jdx<($idx+$howmany+1) and $jdx< scalar(@buff +);$jdx++) { print "\n$buff[$jdx]"; } print "\n". "-" x 20;#Separator } else { print "\n$buff[$idx] <--- does not match"; } } #Start 'shifting' one element out of the array until end of file while(defined($line=<DATA>)) { chomp($line); shift @buff; push(@buff,$line); if($buff[$howmany]=~/$match/) #Always match element at index $howma +ny { print "\n"."*" x 20; #Separator #Print $howmany lines before match for($idx=0;$idx<$howmany;$idx++) { print "\n$buff[$idx]"; } print "\n$buff[$idx] <----- match"; #Print line that matches #Print $howmany lines after match for($idx+=1;$idx<scalar(@buff);$idx++) { print "\n$buff[$idx]"; } print "\n"."*" x 20; #Separator } else { print "\n$buff[$howmany] <--- does not match"; } } print "\n". "=" x 20 . scalar(@buff). " elements";#Separator #At end of file, there are <=(($howmany*2)+1) elements left in @buff #BUt we have already seen $howmany elements in the while loop #So start matching from $howmany+1 for($idx=$howmany+1;$idx<scalar @buff;$idx++) { if($buff[$idx]=~/$match/) { print "\n". "-" x 20;; #Print $howmany lines before match $left=($idx-$howmany); for($jdx=($left)>0?$left:0;$jdx<$idx and $jdx<scalar(@buff);$ +jdx++) { print "\n$buff[$jdx]"; } print "\n $buff[$idx] <---- match"; # print line that matches #Print $howmany lines after match for($jdx=$idx+1;$jdx<($idx+$howmany+1) and $jdx< scalar(@buff +);$jdx++) { print "\n$buff[$jdx]"; } print "\n". "-" x 20;; } else { print "\n$buff[$idx] <--- does not match"; #shift @buff; } } __DATA__ first line contains match second does not third does not either this third line contains one match fourth one again matches fifth sixth seventh eighth ninth tenth eleventh enough! now match something match this too more match ok not any more ENOUGH!!!! no no no no nanananananana hohhohohohohohp lol duck tecm who are you and what nonsense is this hahaha match here!
      that is what i get
      ./ICE_SLM_StrGtw_20120427_01.log <--- does not match ./ICE_SLM_StrGtw_20120427_02.log <--- does not match ./ICE_SLM_StrGtw_20120427_03.log <--- does not match ./ICE_SLM_StrGtw_20120427_04.log <--- does not match ./ICE_SLM_StrGtw_20120427_05.log <--- does not match ./ICE_SLM_StrGtw_20120427_06.log <--- does not match ./ICE_SLM_StrGtw_20120427_07.log <--- does not match ./ICE_SLM_StrGtw_20120427_08.log <--- does not match ./ICE_SLM_StrGtw_20120427_09.log <--- does not match ./ICE_SLM_StrGtw_20120427_10.log <--- does not match ./ICE_SLM_StrGtw_20120427_11.log <--- does not match ./ICE_SLM_StrGtw_20120427_12.log <--- does not match ./ICE_SLM_StrGtw_20120427_13.log <--- does not match ./ICE_SLM_StrGtw_20120427_14.log <--- does not match ====================7 elements ./ICE_SLM_StrGtw_20120427_15.log <--- does not match ./ICE_SLM_StrGtw_20120427_16.log <--- does not match ./ICE_SLM_StrGtw_20120427_17.log <--- does not match