Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much

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...


Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://22267]
and cookies bake in the oven...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (8)
As of 2016-12-07 13:57 GMT
Find Nodes?
    Voting Booth?
    On a regular basis, I'm most likely to spy upon:

    Results (127 votes). Check out past polls.