Bill Harpley wrote:
Hello,
Hello,
I have simple regex problem that is driving me crazy.
I am writing a script to analyse a log file. It contains Java related
information about requests and responses.
Each pair of Request (REQ) and Response (RES) calls have a unique
Request ID. This is a 5 digit hex number contained in square brackets
(e.g. "[81c2d]" ).
Using timestamps in each log entry, I need to calculate the time
difference between the start of the Request and the end of the Response.
As a first step, I thought I would identify the matching REQ/RES pairs
in the log and then set about extracting the timestamp information and
doing the calculations.
I started with a simple script to extract the Request IDs from each log
entry. Here is what one looks like (names have been changed to protect
the innocent).
[2009-01-23 09:20:48,719]TRACE [server-1] [http-80-5] a...@mydomain.net
:090123-092048567:f5825 (SetCallForwardStatusImpl.java:call:54) -
RequestId [81e80] SetCallForwardStatus.REQ { accountNumber:=W12345,
phoneNumber:=12121212121, onBusyStatus:=true, busyCurrent:=voicemail,
onNoAnswerStatus:=false, noAnswerCurent:=voicemail,
onUncondStatus:=false, uncondCurrent:=voicemail }
So I need to extract the 5 hex digits in "RequestId [81e80]". Sounds
simple, eh?
Here is a fragment of my initial script:
You should have the warnings and strict pragmas at the beginning of your
program to let perl help you find mistakes:
use warnings;
use strict;
open ( DATA, "< $INBOX/sample.log") || die "Cannot open source file:
$!";
open ( FILE, "> $INBOX/request.dat") || die "Cannot open request file:
$!";
chomp(@list=<DATA>);
foreach $entry(@list)
{
It looks like you don't really have to read the entire file into memory
in order to process it. You should perhaps use a while loop instead
which will only read one line at a time:
while ( my $entry = <DATA> ) {
chomp $entry;
And you may not need to chomp the current line if you are not accessing
the data at the end of the line.
$entry =~ /\[([a-z0-9]{5})\]/;
You are looking for hexadecimal digits so you want either [a-fA-F0-9] or
[[:xdigit:]] instead.
print "$1\n"; # print to screen
The contents of $1 are only valid if the regular expression matched
successfully, otherwise $1 retains the contents from the previously
successful match.
if ( $entry =~ /RequestId\s+\[([a-fA-F0-9]{5})\]/ ) {
print "$1\n";
}
# print FILE "$1\n"; # print to file
}
John
--
Those people who think they know everything are a great
annoyance to those of us who do. -- Isaac Asimov
--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/