Extract lines from a file

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • joeferns79
    New Member
    • Sep 2008
    • 37

    Extract lines from a file

    I have this log ...

    Code:
    [ERROR] [02 Sep 2009 11:33:17] [Trace] [WebContainer : 54] - infrastructure:ID_UNHANDLED: An un-handled server exception occu
    rred. Please contact your administrator.
            at .util.internal.HandleException.getRemoteException(HandleException.java(Compiled Code))
            at .util.internal.HandleException.getRemoteException(HandleException.java(Compiled Co
    nested exception is:
    infrastructure:RUN_ID_RUNTIME: A runtime exception occurred: javax.transaction.RollbackException.
     at java.lang.reflect.Method.invoke(Method.java(Compiled Code))
            at com.ibm.rmi.util.ProxyUtil$2.run(ProxyUtil.java(Compiled Code))
            at java.security.AccessController.doPrivileged1(Native Method)
            at java.security.AccessController.doPrivileged(AccessController.java(Compiled Code))
            at com.ibm.rmi.util.ProxyUtil.invokeWithPrivilege(ProxyUtil.java(Compiled Code))
            at com.ibm.CORBA.iiop.ClientDelegate.invoke(ClientDelegate.ja
            at .interfaces.Search_TH.invokeStub
    nested exception is:
    javax.transaction.RollbackException
            at com.ibm.ws.Transaction.JTA.TransactionImpl.stage3CommitProcessing(TransactionImpl.java(Compiled Code))
            at com.ibm.ws.Transaction.JTA.TransactionImpl.processCommit(TransactionImpl.java(Compiled Code))
            at com.ibm.ws.Transaction.JTA.TransactionImpl.commit(TransactionImpl.java(Compiled Code))
            at com.ibm.ws.Transaction.JTA.TranManagerImpl.commit(TranManagerImpl.java(Compiled Code))
            at com.ibm.ws.Transaction.JTA.TranManagerSet.commit(TranManagerSet.java(Compiled Code))
            at com.ibm.ws.Transaction.JTA.UserTransactionImpl.commit(UserTransactionImpl.java(Compiled Code))
            at com.ibm.ejs.container.UserTransactionWrapper.commit(UserTransactionWrapper.java(Compiled Code))
            at .util.transaction.ResourcesJTA.M-T00000(ResourcesJTA.java(Compiled Code))
            at .util.transaction.TransactionInfo.commit(TransactionInfo.java(Compiled C
    
    [ERROR] [02 Sep 2009 11:33:18] [Trace.Tools] [WebContainer : 61] - infrastructure:RUN_ID_RECORD_NOT_FOUND: Record not found.
    [ERROR] [02 Sep 2009 11:33:18] [Trace.Tools] [WebContainer : 61] - infrastructure:RUN_ID_RECORD_NOT_FOUND: Record not found.
    I need to input it into a perl script that takes 2 arguments, an input file, i.e. the log above and an output file, that will contain the output,
    eg. Rollback.pl input.log report.log

    First, I need to search for the string "UNHANDLED" and within that stack trace, I need to find the string "RollbackExcept ion". If the "RollbackExcept ion" string is found, I need to find the "_TH" string, that's within "RollbackExcept ion".
    Finally I need to print the date that's on the "UNHANDLED" line and the line that contains the "_TH" string.

    The "UNHANDLED" string could contain more than 1 "RollbackExcept ion" string but I need to find just the 1st instance of "RollbackExcept ion" and then within that find the first instance of "_TH".

    The delimiters could be [ERROR].

    The date and "_TH" string should then be output to the "report.log " file.
  • numberwhun
    Recognized Expert Moderator Specialist
    • May 2007
    • 3467

    #2
    Originally posted by joeferns79
    I have this log ...

    Code:
    [ERROR] [02 Sep 2009 11:33:17] [Trace] [WebContainer : 54] - infrastructure:ID_UNHANDLED: An un-handled server exception occu
    rred. Please contact your administrator.
            at .util.internal.HandleException.getRemoteException(HandleException.java(Compiled Code))
            at .util.internal.HandleException.getRemoteException(HandleException.java(Compiled Co
    nested exception is:
    infrastructure:RUN_ID_RUNTIME: A runtime exception occurred: javax.transaction.RollbackException.
     at java.lang.reflect.Method.invoke(Method.java(Compiled Code))
            at com.ibm.rmi.util.ProxyUtil$2.run(ProxyUtil.java(Compiled Code))
            at java.security.AccessController.doPrivileged1(Native Method)
            at java.security.AccessController.doPrivileged(AccessController.java(Compiled Code))
            at com.ibm.rmi.util.ProxyUtil.invokeWithPrivilege(ProxyUtil.java(Compiled Code))
            at com.ibm.CORBA.iiop.ClientDelegate.invoke(ClientDelegate.ja
            at .interfaces.Search_TH.invokeStub
    nested exception is:
    javax.transaction.RollbackException
            at com.ibm.ws.Transaction.JTA.TransactionImpl.stage3CommitProcessing(TransactionImpl.java(Compiled Code))
            at com.ibm.ws.Transaction.JTA.TransactionImpl.processCommit(TransactionImpl.java(Compiled Code))
            at com.ibm.ws.Transaction.JTA.TransactionImpl.commit(TransactionImpl.java(Compiled Code))
            at com.ibm.ws.Transaction.JTA.TranManagerImpl.commit(TranManagerImpl.java(Compiled Code))
            at com.ibm.ws.Transaction.JTA.TranManagerSet.commit(TranManagerSet.java(Compiled Code))
            at com.ibm.ws.Transaction.JTA.UserTransactionImpl.commit(UserTransactionImpl.java(Compiled Code))
            at com.ibm.ejs.container.UserTransactionWrapper.commit(UserTransactionWrapper.java(Compiled Code))
            at .util.transaction.ResourcesJTA.M-T00000(ResourcesJTA.java(Compiled Code))
            at .util.transaction.TransactionInfo.commit(TransactionInfo.java(Compiled C
    
    [ERROR] [02 Sep 2009 11:33:18] [Trace.Tools] [WebContainer : 61] - infrastructure:RUN_ID_RECORD_NOT_FOUND: Record not found.
    [ERROR] [02 Sep 2009 11:33:18] [Trace.Tools] [WebContainer : 61] - infrastructure:RUN_ID_RECORD_NOT_FOUND: Record not found.
    I need to input it into a perl script that takes 2 arguments, an input file, i.e. the log above and an output file, that will contain the output,
    eg. Rollback.pl input.log report.log

    First, I need to search for the string "UNHANDLED" and within that stack trace, I need to find the string "RollbackExcept ion". If the "RollbackExcept ion" string is found, I need to find the "_TH" string, that's within "RollbackExcept ion".
    Finally I need to print the date that's on the "UNHANDLED" line and the line that contains the "_TH" string.

    The "UNHANDLED" string could contain more than 1 "RollbackExcept ion" string but I need to find just the 1st instance of "RollbackExcept ion" and then within that find the first instance of "_TH".

    The delimiters could be [ERROR].

    The date and "_TH" string should then be output to the "report.log " file.
    Ok, so what have you tried thus far? Please show your code.

    Regards,

    Jeff

    Comment

    • joeferns79
      New Member
      • Sep 2008
      • 37

      #3
      Code:
      use strict;
      
      
      ####################################################################
      # constants
      ####################################################################
      
      use constant COUNT   => 0;
      use constant MAX     => 1;
      use constant MIN     => 2;
      use constant TOTAL   => 3;
      use constant AVERAGE => 4;
      use constant FCOUNT   => 5;
      use constant FMAX   => 6;
      use constant FMIN   => 7;
      use constant FTOTAL  => 8;
      use constant FAVERAGE   => 9;
      
      ####################################################################
      # genreport
      ####################################################################
      
      sub genreport {
         my ($logfile, $reportfile) = @_;
         my ($line, $elapsed, $facade, %calls);
         my (@fields, $count, $min, $max, $total, $average,$fcount,$fmin,$fmax,$ftotal,$faverage);
         my (@list, $totalcalls);
         my ($falseflag);
         my ($statdate,$server);
         my ($starttime,$error,$first);
        open(LOGFILE, "<$logfile")
            or die "Can't open $logfile: $!";
      
         open(REPORTFILE, ">$reportfile")
            or die "Can't open $reportfile: $!";
      
         $totalcalls = 0;
         while ($line = <LOGFILE>) {
            chomp $line;
            $falseflag= 0;
            $statdate = substr($line,9,11);
            $starttime = substr($line,21,8);
          if($line =~(/UNHANDLED/))
           {
       while ($line =<LOGFILE>){
       if($line =~(/RollbackException/))
            {
         $falseflag = 1;
      while ($line =<LOGFILE>){
       if($line =~(/_TH/)) {
               $line = substr($line,3,length($line));
               printf "%s, %s,%s\n",$statdate,$starttime,$line;
               last;
              }
            }
          }
       }
      }
      }
      }
      ####################################################################
      # main
      ####################################################################
      
      if (@ARGV != 2) {
         die "usage: servercalls.pl <logfile> <reportfile>";
      }
      
      my ($logfile, $reportfile) = @ARGV;
      
      genreport($logfile, $reportfile);

      Comment

      • KevinADC
        Recognized Expert Specialist
        • Jan 2007
        • 4092

        #4
        How come you have not added the filehandle name where you want to redirect output to a file like Jeff showed you?

        Comment

        • joeferns79
          New Member
          • Sep 2008
          • 37

          #5
          Sorry, the one I made a change to was another script. Here's this one with the change....

          Code:
          use strict; 
            
            
          #################################################################### 
          # constants 
          #################################################################### 
            
          use constant COUNT   => 0; 
          use constant MAX     => 1; 
          use constant MIN     => 2; 
          use constant TOTAL   => 3; 
          use constant AVERAGE => 4; 
          use constant FCOUNT   => 5; 
          use constant FMAX   => 6; 
          use constant FMIN   => 7; 
          use constant FTOTAL  => 8; 
          use constant FAVERAGE   => 9; 
            
          #################################################################### 
          # genreport 
          #################################################################### 
            
          sub genreport { 
             my ($logfile, $reportfile) = @_; 
             my ($line, $elapsed, $facade, %calls); 
             my (@fields, $count, $min, $max, $total, $average,$fcount,$fmin,$fmax,$ftotal,$faverage); 
             my (@list, $totalcalls); 
             my ($falseflag); 
             my ($statdate,$server); 
             my ($starttime,$error,$first); 
            open(LOGFILE, "<$logfile") 
                or die "Can't open $logfile: $!"; 
            
             open(REPORTFILE, ">$reportfile") 
                or die "Can't open $reportfile: $!"; 
            
             $totalcalls = 0; 
             while ($line = <LOGFILE>) { 
                chomp $line; 
                $falseflag= 0; 
                $statdate = substr($line,9,11); 
                $starttime = substr($line,21,8); 
              if($line =~(/UNHANDLED/)) 
               { 
           while ($line =<LOGFILE>){ 
           if($line =~(/RollbackException/)) 
                { 
             $falseflag = 1; 
          while ($line =<LOGFILE>){ 
           if($line =~(/_TH/)) { 
                   $line = substr($line,3,length($line)); 
                   printf REPORTFILE "%s, %s,%s\n",$statdate,$starttime,$line; 
                   last; 
                  } 
                } 
              } 
           } 
          } 
          } 
          } 
          #################################################################### 
          # main 
          #################################################################### 
            
          if (@ARGV != 2) { 
             die "usage: servercalls.pl <logfile> <reportfile>"; 
          } 
            
          my ($logfile, $reportfile) = @ARGV; 
            
          genreport($logfile, $reportfile);

          Comment

          • joeferns79
            New Member
            • Sep 2008
            • 37

            #6
            Somehow, the line that's supposed to be printed just once is getting repeated 4 times, since the line with the "_TH" string is present in 4 locations under the stack trace.

            i.e. the input log looks like this...

            Code:
            [ERROR] [02 Sep 2009 11:33:17] [Trace] [WebContainer : 54] - infrastructure:ID_UNHANDLED: An un-handled server exception occu 
            rred. Please contact your administrator. 
                    at .util.internal.HandleException.getRemoteException(HandleException.java(Compiled Code)) 
                    at .util.internal.HandleException.getRemoteException(HandleException.java(Compiled Co 
            at .interfaces.Search_TH.invokeStub 
            nested exception is: 
            infrastructure:RUN_ID_RUNTIME: A runtime exception occurred: javax.transaction.RollbackException. 
             at java.lang.reflect.Method.invoke(Method.java(Compiled Code)) 
                    at com.ibm.rmi.util.ProxyUtil$2.run(ProxyUtil.java(Compiled Code)) 
                    at java.security.AccessController.doPrivileged1(Native Method) 
                    at java.security.AccessController.doPrivileged(AccessController.java(Compiled Code)) 
                    at com.ibm.rmi.util.ProxyUtil.invokeWithPrivilege(ProxyUtil.java(Compiled Code)) 
                    at com.ibm.CORBA.iiop.ClientDelegate.invoke(ClientDelegate.ja 
                    at .interfaces.Search_TH.invokeStub 
            nested exception is: 
            javax.transaction.RollbackException 
                    at com.ibm.ws.Transaction.JTA.TransactionImpl.stage3CommitProcessing(TransactionImpl.java(Compiled Code)) 
                    at com.ibm.ws.Transaction.JTA.TransactionImpl.processCommit(TransactionImpl.java(Compiled Code)) 
                    at com.ibm.ws.Transaction.JTA.TransactionImpl.commit(TransactionImpl.java(Compiled Code)) 
                    at com.ibm.ws.Transaction.JTA.TranManagerImpl.commit(TranManagerImpl.java(Compiled Code)) 
                    at com.ibm.ws.Transaction.JTA.TranManagerSet.commit(TranManagerSet.java(Compiled Code)) 
                    at com.ibm.ws.Transaction.JTA.UserTransactionImpl.commit(UserTransactionImpl.java(Compiled Code)) 
                    at com.ibm.ejs.container.UserTransactionWrapper.commit(UserTransactionWrapper.java(Compiled Code)) 
                    at .util.transaction.ResourcesJTA.M-T00000(ResourcesJTA.java(Compiled Code)) 
                    at .util.transaction.TransactionInfo.commit(TransactionInfo.java(Compiled C 
              at .interfaces.Search_TH.invokeStub 
            nested exception is: 
            javax.transaction.RollbackException 
                    at com.ibm.ws.Transaction.JTA.TransactionImpl.stage3CommitProcessing(TransactionImpl.java(Compiled Code)) 
                    at com.ibm.ws.Transaction.JTA.TransactionImpl.processCommit(TransactionImpl.java(Compiled Code)) 
                    at com.ibm.ws.Transaction.JTA.TransactionImpl.commit(TransactionImpl.java(Compiled Code)) 
                    at com.ibm.ws.Transaction.JTA.TranManagerImpl.commit(TranManagerImpl.java(Compiled Code)) 
                    at com.ibm.ws.Transaction.JTA.TranManagerSet.commit(TranManagerSet.java(Compiled Code)) 
                    at com.ibm.ws.Transaction.JTA.UserTransactionImpl.commit(UserTransactionImpl.java(Compiled Code)) 
                    at com.ibm.ejs.container.UserTransactionWrapper.commit(UserTransactionWrapper.java(Compiled Code)) 
                    at .util.transaction.ResourcesJTA.M-T00000(ResourcesJTA.java(Compiled Code)) 
                    at .util.transaction.TransactionInfo.commit(TransactionInfo.java(Compiled C 
              at .interfaces.Search_TH.invokeStub 
            nested exception is: 
            javax.transaction.RollbackException 
                    at com.ibm.ws.Transaction.JTA.TransactionImpl.stage3CommitProcessing(TransactionImpl.java(Compiled Code)) 
                    at com.ibm.ws.Transaction.JTA.TransactionImpl.processCommit(TransactionImpl.java(Compiled Code)) 
                    at com.ibm.ws.Transaction.JTA.TransactionImpl.commit(TransactionImpl.java(Compiled Code)) 
                    at com.ibm.ws.Transaction.JTA.TranManagerImpl.commit(TranManagerImpl.java(Compiled Code)) 
                    at com.ibm.ws.Transaction.JTA.TranManagerSet.commit(TranManagerSet.java(Compiled Code)) 
                    at com.ibm.ws.Transaction.JTA.UserTransactionImpl.commit(UserTransactionImpl.java(Compiled Code)) 
                    at com.ibm.ejs.container.UserTransactionWrapper.commit(UserTransactionWrapper.java(Compiled Code)) 
                    at .util.transaction.ResourcesJTA.M-T00000(ResourcesJTA.java(Compiled Code)) 
                    at .util.transaction.TransactionInfo.commit(TransactionInfo.java(Compiled C 
              at .interfaces.Search_TH.invokeStub 
            
            [ERROR] [02 Sep 2009 11:33:18] [Trace.Tools] [WebContainer : 61] - infrastructure:RUN_ID_RECORD_NOT_FOUND: Record not found. 
            [ERROR] [02 Sep 2009 11:33:18] [Trace.Tools] [WebContainer : 61] - infrastructure:RUN_ID_RECORD_NOT_FOUND: Record not found.
            I want to extract the line that contains the "_TH" string only from the 1st instance after finding the 1st "RollbackExcept ion" string.

            Comment

            Working...