Match and Extract everything between 2 words in a file

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

    #16
    KevinADC,
    I tried your suggestion too, and that didn't return anything either ...

    Code:
    use strict;
    use warnings;
    
    
    ####################################################################
    # genreport
    ####################################################################
      sub genreport {
    
       my ($logfile, $line, @message); 
       
       open(LOGFILE, "<$logfile")
          or die "Can't open $logfile: $!";
    
          
    OUTTER: while ($line = <LOGFILE>) { 
             if ($line =~ /^\[ERROR\](.*?)infrastructure:ID_UNHANDLED:/) { 
               push @message, $line; 
             while ($line = <LOGFILE>) { 
              redo OUTTER if ($line =~ /^\[ERROR\]/); 
             push @message, $line; 
          } 
       } 
    } 
    
    print "@message";
    
    }

    Comment

    • Kelicula
      Recognized Expert New Member
      • Jul 2007
      • 176

      #17
      Hummm....
      How are you calling on genreport?
      What exactly do you pass to it?
      Is it in the current package, or a separate file?

      Very strange.

      Have you tried KevinADC's code?
      It looks like it works too. (using too objectively..:)

      I swear my code worked in Komodo with Perl 5.10 on Windows.

      Of course I didn't have the exact same logfile to scan.

      Comment

      • joeferns79
        New Member
        • Sep 2008
        • 37

        #18
        I am calling it through a batch file in a command prompt in Windows and pass the name of the log file, i.e.

        UAESummary.bat AppLog.txt > Report.log


        Yes, I tried KevinADC's code too, to no avail.

        Comment

        • Icecrack
          Recognized Expert New Member
          • Sep 2008
          • 174

          #19
          he means how are you running the Perl script
          if the script is being run by that Batch file, may you post the code to that batch file here.
          in [CODE*] [/CODE*] Tags (take out the * for code tags to work)

          Comment

          • joeferns79
            New Member
            • Sep 2008
            • 37

            #20
            Here's the batch file I call...
            Code:
            @echo off
            
            set PERL_HOME=C:\Perl
            set PATH=%PERL_HOME%\bin;%PATH%
            
            set script=UAESummary.bat
            
            
            
            perl UAESummary2.pl %1
            
            if errorlevel 1 (
               echo.
               echo %script%: download failed
               exit /b 1
            )
            Last edited by numberwhun; May 20 '09, 03:36 AM. Reason: PLEASE use code tags!

            Comment

            • Icecrack
              Recognized Expert New Member
              • Sep 2008
              • 174

              #21
              I would put the path to the log file under
              Code:
              my ($logfile, $line, @message);
              add
              Code:
              $logfile='PATH\TO\Report.log';

              Comment

              • joeferns79
                New Member
                • Sep 2008
                • 37

                #22
                No help there ...

                Code:
                use strict; 
                use warnings; 
                  
                  
                #################################################################### 
                # genreport 
                #################################################################### 
                  sub genreport { 
                  
                   my ($logfile, $line, @message);  
                  
                   $logfile='C:\PerlScripts\CuramApp.log';
                
                    
                  
                OUTTER: while ($line = $logfile) {  
                         if ($line =~ /^\[ERROR\](.*?)infrastructure:ID_UNHANDLED:/) {  
                           push @message, $line;  
                         while ($line = $logfile) {  
                          redo OUTTER if ($line =~ /^\[ERROR\]/);  
                         push @message, $line;  
                      }  
                   }  
                }  
                  
                print "@message"; 
                  
                }
                Also, tried ...

                Code:
                use strict; 
                use warnings; 
                  
                  
                #################################################################### 
                # genreport 
                #################################################################### 
                  sub genreport { 
                  
                   my ($logfile, @message);  
                  
                   $logfile='C:\PerlScripts\CuramApp.log';
                  
                   my $file = do { local $/; $logfile }; 
                  
                   while($file =~ /^(\[ERROR\](?-s:.*)infrastructure:ID_UNHANDLED.*?infrastructure:RUN_ID_RUNTIME.*?)\n\n/mgs) {  
                push(@message, $1);  
                }  
                  
                print "@message"; 
                  
                }

                Comment

                • Icecrack
                  Recognized Expert New Member
                  • Sep 2008
                  • 174

                  #23
                  are you ever calling your Sub ? ever ?


                  Add this above sub genreport {
                  Code:
                  &genreport;

                  Comment

                  • joeferns79
                    New Member
                    • Sep 2008
                    • 37

                    #24
                    Sorry for the delay in replying but yes, I am. I am still not seeing any output...

                    Code:
                    use strict;
                    
                    
                    
                    ####################################################################
                    # genreport
                    ####################################################################
                      sub genreport {
                       my ($logfile, $reportfile) = @_;
                       my ($line, @message, @summary, $count, $match, $n);
                          
                       open(LOGFILE, "<$logfile")
                          or die "Can't open $logfile: $!";
                          
                       open (REPORTFILE, ">$reportfile")
                          or die "Can't open $reportfile: $!";
                          
                    
                       $count = $match = 0;
                       while ($line = <LOGFILE>) {
                         # chomp $line;
                        if ($line =~ /^(\[ERROR\](?-s:.*)infrastructure:ID_UNHANDLED.*?infrastructure:RUN_ID_RUNTIME.*?)\n\n/mgs) {
                             push @message, $line;
                             $match = 1;
                             next;
                            }
                        
                        }
                       for (@message) {
                       print;
                     }
                    
                    }
                    
                    ####################################################################
                    # main
                    ####################################################################
                    
                    if (@ARGV != 2) {
                       die "usage: UAESummary.pl <logfile> <reportfile>";
                    }
                       
                    my ($logfile, $reportfile) = @ARGV;
                    
                    genreport($logfile, $reportfile);
                       
                    exit 0;

                    Comment

                    • Icecrack
                      Recognized Expert New Member
                      • Sep 2008
                      • 174

                      #25
                      sorry for the delay on my end i have been testing this for weeks now, and just found the answer but first i need to know how your logs are set out is it 1 big single line or is it a multi-lined?
                      past a log file here as a file attachment

                      Comment

                      • joeferns79
                        New Member
                        • Sep 2008
                        • 37

                        #26
                        Icecrack, I was going through the forum and I noticed that you had posted a reply. I know it's like 2 months since you replied and I am not sure if you'll get this, but yes, it's a multi-line log.

                        Comment

                        Working...