Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>So your question looks like this, if I understand correctly:</p> <ul> <li>The log file has a number of sections seperated by double newline.</li> <li>Each is headed by a line with a date etc..</li> <li>We are only interested in the sections whose headers match <code>/BaseServlet.*?Site-102/</code>.</li> <li>If the body of a section matches <code>/^TechnicalDifficultiesException: TD page delivered by handleThrowable/</code>, we want to select the body of the previously matched section, which we should maybe validate to look like a java exception.</li> <li>We process the whole log file, and return the last exception found this way.</li> </ul> <p>Fair enough.</p> <pre><code>#!/usr/bin/perl use strict; use warnings; local $/ = ""; # paragraph mode my ($prev_sec, $prev_err); SECTION: while (my $head = &lt;&gt;) { my $body = &lt;&gt;; defined $body or die "Can't read from empty filehandle."; next SECTION unless $head =~ /BaseServlet.*?Site-102/; if ($body =~ /^TechnicalDifficultiesException: TD page delivered by handleThrowable/) { $prev_err = $prev_sec; } $prev_sec = $body; } die "No error found" unless defined $prev_err; print $prev_err; </code></pre> <p>(not really tested that much, but prints out the error from your snippet)</p> <p>The code is a bit to long for a one-liner. You could always pipe the source into the perl interpreter, if you wanted.</p> <pre><code>perl -ne'BEGIN{$/=""}END{print$prev_err}$b=&lt;&gt;;defined$b or die"empty FH";/BaseServlet.*?Site-102/ or next;$prev_err=$prev_sec if $b=~/^TechnicalDifficultiesException: TD page delivered by handleThrowable/;$prev_sec=$b' </code></pre> <p>Specify the log file as a command line argument, or pipe the file contents directly into that program. Finding the correct log file isn't hard. In a snippet of Perl:</p> <pre><code>my $log_dir = ...; my ($log) = sort glob "$log_dir/LOG*"; die "no log in $log_dir" unless defined $log; </code></pre> <hr> <h3>Update</h3> <p>If the date should be captured as well, the code would change to</p> <pre><code>#!/usr/bin/perl use strict; use warnings; local $/ = ""; # paragraph mode my (@prev, @prev_err); SECTION: while (my $head = &lt;&gt;) { my $body = &lt;&gt;; defined $body or die "Can't read from empty filehandle."; next SECTION unless $head =~ /BaseServlet.*?Site-102/; if ($body =~ /^TechnicalDifficultiesException: TD page delivered by handleThrowable/) { @prev_err = @prev; } @prev = ($head, $body); } die "No error found" unless @prev_err; my ($date) = $prev_err[0] =~ /^(\d{4}-\d\d-\d\d \d\d:\d\d:\d\d),/; print "$date\n\n$prev_err[1]"; </code></pre> <p>And as the one-liner:</p> <pre><code>perl -ne'BEGIN{$/=""}END{@perr||die"No error found";($date)=$perr[0]=~/^(\d{4}-\d\d-\d\d \d\d:\d\d:\d\d),/;print"$date\n\n$perr[1]"}$b=&lt;&gt;;defined$b or die"empty FH";/BaseServlet.*?Site-102/ or next;@perr=@p if $b=~/^TechnicalDifficultiesException: TD page delivered by handleThrowable/;@p=($_,$b)' </code></pre> <p>I don't understand how it could only return the first match; this code should process the whole file. If you could provide a more complete testcase, I could verify that this code works as required.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload