Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re^2: How can I print three lines before pattern match?

by Narveson (Chaplain)
on Aug 14, 2009 at 05:24 UTC ( [id://788515]=note: print w/replies, xml ) Need Help??


in reply to Re: How can I print three lines before pattern match?
in thread How can I print three lines before pattern match?

Or if you don't want to test the length of your array forevermore:

my @history = ("\n") x 3; while (<>) { print @history if /pattern/; push @history, $_; shift @history; }

Replies are listed 'Best First'.
Re^3: How can I print three lines before pattern match?
by neurotoxx (Novice) on Aug 14, 2009 at 14:00 UTC

    This works very well, thank all of you for your help.

    Where can I place a print statement that seperates the output results for each match?

    #!/Perl/bin/perl -w open(INFILE, "< file.log") or die "Cant open file : $!"; open(OUT, "> results.txt") or die "Cant open new file : $!"; use strict; my $pattern = "Error while detecting new item"; my @history = ("\n") x 3; while (<INFILE>) { print OUT @history if /$pattern/; push @history, $_; shift @history; } print $.; close INFILE; close OUT; exit;

    Example:

    results.txt matched line 1 matched line 2 matched line 3 results of first match matched line 1 matched line 2 matched line 3 results of second match and so on...

    Thank you again so much, Neurotoxx

Re^3: How can I print three lines before pattern match?
by ikegami (Patriarch) on Aug 14, 2009 at 14:56 UTC
    ++, but that should be my @history = ('') x 3;

      What does the '' vs. "\n" do?

      How could I print also the same line that has the pattern match?

      Thank you Sifu,<\p>

      Neurotoxx

        Print the empty string prints nothing. Printing a newline prints an empty line.
        my @history = ('') x 3; while (<>) { print @history, $_ if /pattern/; push @history, $_; shift @history; }
        or
        my @history = ('') x 4; while (<>) { push @history, $_; shift @history; print @history, $_ if /pattern/; }

      Thank you very much for your help.

      I'm sorry to bother, but how can I add the existing matched line with the three lines?

      ++, but that should be my @history = ('') x 3;
      ++, but that should be my @history = ('') x 3 + $.; #???

      Thank you agian.

        Figured it out...to add the line that contains the match string, I added an additional line to the loop. Probably not the best way, but it works.

        while (<$_>) { print OUT @history if /$pattern/; print OUT $_ if /$pattern/; # adds line with match push @history, $_; shift @history; }

        Thanks again every one,

        Neurotoxx

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://788515]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (2)
As of 2024-04-19 19:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found