Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling

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 all is quiet...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (2)
As of 2018-01-21 06:59 GMT
Find Nodes?
    Voting Booth?
    How did you see in the new year?

    Results (227 votes). Check out past polls.