Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister

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]
[Discipulus]: no, seriously thanks both, i missed 'scientist' and 'floating point' and the resarche was imposible..
Discipulus go to his cell to bookmark this
[haukex]: Well to be honest I just did a super search for "every should know floating" ;-)
[Discipulus]: floating was the keyword and.. to use supersearch is plus sign not a (plus) sin

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (6)
As of 2017-06-26 20:56 GMT
Find Nodes?
    Voting Booth?
    How many monitors do you use while coding?

    Results (594 votes). Check out past polls.