Sorting lines with regex in perl -


I am trying to read a log file and write all error logs in a new file. I should also keep in mind that how many errors are there and the number of messages. I should acknowledge that the log will break across multiple lines, so I'm using a variable regex series to find all possibilities and type the appropriate file handles my file: FILE, ERRORFILE, and SUCCESSFILE.

  Strictly use; Use warnings; My $ total count = 0; My $ error number = 0; My $ log = "s"; # $ Log controls whether what should be written, where it does not start with code # "S" = Successful file, "e" = error, file my $ logStart = "y"; # M / ^ I / or m / ^ E / is used with full code # The code has been broken into pieces. My $ dash = 0; While (& lt; FILE & gt;) {$ dash + = () = $ _ = ~ m / - / g; #tr can not be used /// Because it is counted on compilation ($ dash lt 25) {Next; } # Leaves this line --- --- Start & lt; Repository & gt; --- "Elesif (m / [a-zA-Z & lt; & gt;] / & amp; amp; amp; $ dash lt 25) {next;}; Alsf ($ Dash & gt = 26) {final ;} "End And Lieutenant; Repository & gt; ## Loop Loop {$ log = "s"; $ logStart = "n"; $ TOTALCOUNT ++; Upcoming;} ################################################################################ unknown Logs elsif (M / ^ & e and amp; $ Logstart Gathering "ignore Y") chomp {$ _; print ERRORFILE "$ _"; $ ERRORCOUNT ++; $ TOTALCOUNT ++; $ Log = "e "; $ logStart =" n ";} Alsf (M / \. \ N $ /) or of {# log if ($ login eq" s ") {$ logStart =" y "; next;} print ERRORFILE" $ _ \ n "If $ log eq" e "; $ logStart =" y ";} and {# line does not begin with code or finally". \ N "chomp $ _; print $ ERRORFILE" $ _ "if $ log eq" e "; Next if $ log eq" s ";}} print" \ n $ error-proof error logs are logged. \ N "; Print" $ log in total log files are logged. \ N ";   

I know that non-error logs begin with I00020036 and errors start with E03020039. Both of them" Finally. \ N "

  ---------- Start> Load Store & gt; --------------- I00020036: loaded 'C: \ documents and Settings \ Dorja03 \ Desktop \ DSMProduct \ external \ etpki \ Linux_2.4_x86 \ redistrib \ readme.txt \ DSM R11 \ external \ Etpki \ Linux_2.4_x86 \ redistrib \ readme.txt. E03020039: C: \ Documents and Settings \ dorja03 \ Desktop \ DSMProduct \ external \ etpki \ Linux_2.4_x86 \ redistrib \ etpki_install_lib.sh the \ DSM R11 \ external \ etpki \ Linux_2.4_x86 \ redistrib \ etpki_install_lib.sh unable to load in. text file Contains invalid characters ---------- End and Left Load Loader & gt; ---------------   

I have two A test with rows Running the Moon If the error first appears, then it prints the error with a non-error log in the file, and on the same line. If the non-error goes first, then it does not recognize the error. / P>

Is this because I'm using M / wrong or something else?

EDIT: The test input has been added. I also added the code to leave the header and footer.

Test Output: If a non-error occurs first, there are 0 errors and the total is 1 log if non-error occurs first, then there is 1 error and 1 total log.

If this works, then it should be said that there were 1 error and 2 logs, it would also print the error only on ERRORFILE.

The answer is no why your code is not working, but here's the How I will face this problem:

  • Since logs can span multiple lines, modify default line-by-line behavior by tweaking.

  • Use appropriate data structures to filter out errors from non-errors, this will allow you to postpone printing later.

    Then the code will look something like this:

      Use strict; Use warnings; My% log; Local $ / = ". \ N"; While (<< now $ _ is full (multi-line) log next if / --- start /; # If / start / end / if --- / end /; # Stop the process / end / if (m / ^ I /) {push @ {$ log {nonerror}}, $ _; } If (m / ^ e /) {push @ {$ log {error}}, $ _; }} Printf "% d errors are logged \ n", scaler @ {$ logs {error} // []}; Printf "The full logfile has% d logs. \ N", @ {$ logs {error} // []} + @ {$ logs {nonerror} // []};   

    I like about this approach:

    • Perl takes care of deciding on the end of each log message ( $ logStart variable fully).
    • Logic is very easy to expand.
    • While the loop log file is dedicated to processing (no need ++ anything).
    • Using intelligently labeled data structures instead of the temporary variable makes it easy for code maintenance.

Comments

Popular posts from this blog

Verilog Error: output or inout port "Q" must be connected to a structural net expression -

jasper reports - How to center align barcode using jasperreports and barcode4j -

c# - ASP.NET MVC - Attaching an entity of type 'MODELNAME' failed because another entity of the same type already has the same primary key value -