Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Re: Parsing a log file

by ahunter (Monk)
on Jul 13, 2000 at 00:16 UTC ( #22267=note: print w/ replies, xml ) Need Help??


in reply to Parsing a log file

I think what you're looking for is to use a hash to store the values, and a regexp or two to parse the values:

use strict; my %count = (); # Read the stuff from STDIN while (<STDIN>) { if (/GET .*(\.[^\s]+) ([0-9]{3})/) { my ($ftype, $reason) = ($1, $2); $count{"$ftype-$reason"}++; } } # Now print the stuff out foreach (keys(%count)) { if (/^(.*)-(.*)$/) { print "Filetype $1, reason $2 has a count of $count{$_}\n"; } }
See perlre for details on regular expressions and perldata for information about hashes.

There is also an evil hacky way of doing this, which is much faster, but requires you to know which filetypes and reason codes you are looking for.

use strict; { local $/ = undef; # Slurp mode my $file = <STDIN>; my $html = $file=~s/\.html 200//g; print "Count of HTML 200 is $html\n"; }
This works because s/// returns the number of items replaced, and is surprisingly fast at doing this on large files...

Andrew.


Comment on Re: Parsing a log file
Select or Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://22267]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (7)
As of 2014-08-20 12:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (113 votes), past polls