
Laurent_R
Enthusiast
Nov 25, 2012, 2:23 AM
Post #2 of 2
(539 views)
|
|
Re: [laknar] regexp to search through log files and convert into pipe delimited file?.
[In reply to]
|
Can't Post
|
|
There are two possible approaches: one, if your log file is not too large, is to "slurp" the whole file into an array of lines and then process each line. The advangtage of this approach is that when you read a line with one of your key word, it is very easy to get back to the previous line to collect the date. The other approach, which I usually prefer because I am most of the time dealing with very large files whose size may or will exceed the size of the memory available to my process, is to read the lines one at a time, but always storing the previous line, so as to be able to pick up the date of the previous line if the current line has one of the searched key word. Adopting the second approach, you could try something like this (untested, there may be a couple of typos, but you should get the basic idea):
my $previous line; my $date_regex = qr /^\w{3} \d?\d, \d{4} \d?\d:\d\d:\d\d (:?AM|PM)/; while (my $line = <$INPUT>) { chomp $line; $previous line = $line; if ($line =~ /(Authenticate Re(:?quest|sponse)XML/ or /Authorize Re(:?quest|sponse)XML)/) { my $type_msg = $1; } else { next; }; my $diagnostic = split /:/, $line, 1; my $date = $1 if $previous_line =~ /($date_regex)/; # we have found date and diagnostic, we need to loop for the order id while (<$INPUT>) { next unless /OrderID/; $orderID = $1 if /^<OrderID>(\d+)<\\OrderID>/; print $date, "|", $diagnostic, "|", $type_msg, "|", $orderID, "\n"; last; } }
(This post was edited by Laurent_R on Nov 25, 2012, 2:24 AM)
|