[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

[FWP] Parsing log files



Colleague evinced a desire to parse web server log files into a hash of 
arrays (questionable format, but customer is always right, etc :-).  I was 
astounded that I couldn't find anything on CPAN, but it turned out to be 
easy enough.  The first line specifies the format of the rest of the lines, 
with %attr% naming an attribute, everything else being literal 
text.  Colleague wanted attr to be a key in the hash.

use Data::Dumper;

$_ = <DATA>;
s/format=(.*)\n/$1/;
my @keys = /%(.*?)%/g;
s/(.*?)%(?:.*?)%(.*?)/\Q$1\E(.*?)\Q$2\E/g;
my $format = qr/^$_$/;

my %data;
while (<DATA>) {
   chomp;
   if (/$format/) {
     my $i = 0;
     push @{$data{$_}}, eval '$'.++$i for @keys;
   }
   else {
     print STDERR "*** No match: $_\n";
   }
}

print Dumper \%data;

__END__
format=%a% - %b% [%c%] "%d%" %e% %f%
blah.foo.com - - [13/Jan/2000:00:32:47 -0800] "GET / HTTP/1.0" 200 2943
blech.foo.com - - [13/Jan/2000:00:32:48 -0800] "GET / HTTP/1.0" 200 2943



--
Peter Scott
Pacific Systems Design Technologies


==== Want to unsubscribe from Fun With Perl?  Well, if you insist...
==== Send email to <fwp-request@technofile.org> with message _body_
====   unsubscribe