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

Hi, I am writing a script that opens an exception log as a filehandle, searches for Java exceptions, and prints them to a log file. However, I would like the script to print a blank line after the last line of the exception. As you may or may not know, Java exceptions can be variable length. So far, here is the code:
open (DIFFR, "javaex.log"); while (<DIFFR>) { $line=$_; #read first line if ($line =~ /at / || $line =~ /xception/) { chomp $line; print LOGFILE "$line,\n"; } }
The reason why I want to have a blank line after the exception is to separate the exceptions in LOGFILE. Thanks!

Replies are listed 'Best First'.
Re: Adding a blank line after last line of match
by ikegami (Patriarch) on Jan 27, 2005 at 18:21 UTC

    How do you define the end of the exception? I'm going to assume you want to insert a blank link before the first line that doesn't match in a group of successive matches.

    my $blank_needed = 0; open(DIFFR, "javaex.log"); while (<DIFFR>) { if (/at / || /xception/) { chomp; print LOGFILE "$_,\n"; $blank_needed = 1; } else { print("\n") if $blank_needed; $blank_needed = 0; } } print("\n") if $blank_needed;
Re: Adding a blank line after last line of match
by holli (Abbot) on Jan 27, 2005 at 19:47 UTC
    use strict; my $ex = 0; while (<DATA>) { if ( /^java/ && /Exception$/ ) { print "\n" if $ex; $ex++; } print; } __DATA__ java.io.IOException at com.mycompany.MyClass.myMethod(com.mycompany.MyClass.java:3 +2) at com.mycompany.MyClass.main(com.mycompany.MyClass.java:18) java.lang.IllegalStateException at com.mycompany.MyClass.myMethod(com.mycompany.MyClass.java:25 +) at com.mycompany.MyClass.main(com.mycompany.MyClass.java:18)

    holli, regexed monk