regex - Perl Program to parse through error log file, extract error message and output to new file -


i need write perl program parse through error log , output error messages new file. having issues setting regex this. in error log, error code starts word "error" , end of each error message ends ". " (period , space). want find errors, count them, , output entire error message of each error message new file.

i tried having issues:

open(fh,"<$filetoparse");      $outputfile='./errorlog.txt';     open(output,">$outputfile");     $errorstart='error';     $errorend=". ";      while(<fh>)     {     if (fh=~ /^\s*$errorstart/../$errorend/)        {         print output "success";     }        else     {         print output "failed";     }     }  } 

the $errorstart , $errorend saw online , not sure if correct way code it.

also know printing "success" or "failure" not said looking for, added in confirmed code works, haven't tried coding counting error messages yet.

before snippet of code have print statement asking user location address of .txt file want parse. confirmed particular section of code words properly. help! let me know if more info needed!

here example of data using:

sample data

-----begin load-----
success: file loaded .
success: file loaded .
success: file loaded .
success: file loaded .
success: file loaded .
success: file loaded .
error: file unable load unknown reason .
success: file loaded .
success: file loaded .
error: file unable load example of log file span
multiple lines .
success: file loaded .
------end load-------

while log may not need span multiple lines, there data throughout log similar how above. every message logged starts either success or error , message done when " . " (whitespace-period-whitespace) encountered. log want parse through 50,000 entries long needless code identify multi-line error msgs output entire multi-line message output file.

update

i have written code reason won't work. think has delimiter can't figure out is. file using has messages separated "whitespace period newline". can see i'm doing wrong??

{ local $/ = " .\n"; if ($outputtype == 1) {     $outputfile="errorlog.txt";     open(output,">>$outputfile");     $errorcount=0;     $errortarget="error";     print output "-----------error log-----------\n";     {     while(<fh>)     {     if ($_ =~ m/^$errortarget/)     {         print output "$_\n";         print output "next code is: \n";         $errorcount++;     }     }     print output "\nerror count : $errorcount\n";     } } } 

there several problems code start off.

  • always use strict; , use warnings;.
  • 3 argument open less error prone. open ( $fh, "<", $filename ) or die $!;
  • always check open worked.
  • fh =~ doesn't think does.
  • range operator tests if you're between 2 chunks of text in code. particularly relevant multi-line operations. if error log isn't, it's not need.

assuming you've error data this:

error: broken. warning: might broken. info: not broken. error: still broken. 

this code trick:

use strict; use warnings;  $filetoparse = "myfile.txt"; $outputfile  = "errorlog.txt";  open( $input,  "<", $filetoparse ) or die $!; open( $output, ">", $outputfile )  or die $!;  $count_of_errors = 0; #set record delimiter local $/ = " . \n";  while ( $lines = <$input> ) {     $lines =~ s/^-----\w+ load-----\n//g; #discard 'being/end load' lines.      if ( $lines =~ m/^error/ ) {         $count_of_errors++;         print {$output} $lines;     } } close ( $input ); close ( $output );  print "$count_of_errors errors found\n"; 

if you've multi-line error message, you'll need different approach though.


Comments

Popular posts from this blog

asp.net mvc - SSO between MVCForum and Umbraco7 -

Python Tkinter keyboard using bind -

ubuntu - Selenium Node Not Connecting to Hub, Not Opening Port -