scryptKiddy scryptKiddy - 5 months ago 10
Perl Question

How to add values from duplicate keys in Perl hash

Every week I want to parse through the daily logs (on a Red Hat system) and get some statistics on the IP address column. The daily logs contain data with

<device>,<ip>,<city>,<packets>


like this:

Sample data - First 5 lines of one of the logs:



gw1,25.0.10.61,houston,50
gw1,25.0.20.61,dallas,30
gw1,25.0.30.60,ftworth,80
gw1,25.0.10.61,houston,40
gw1,25.0.10.62,houston,40


I'd like to go through all seven logs and determine the total number of packets for each IP address.

The desired output would be

<ip>,<packet_count>


sorted by packet count from all the seven logs like so:

25.0.10.61,480
25.0.10.62,400
25.0.30.60,220


etc.

I'm not quite sure if a hash is the best way to do this, and if it is, how to approach it.

Answer

Here's how I would do this:

my $packetCountByIP = {};
for (my $i = 1; $i <= 7; ++$i) {
    my $fileName = 'activity'.$i.'.log';
    my $fh;
    if (!open($fh,'<',$fileName)) { die("$fileName: $!"); }
    while (my $line = <$fh>) {
        my $fields = [split(',',$line)];
        my $ip = $fields->[1];
        my $packetCount = $fields->[3]+0;
        $packetCountByIP->{$ip} += $packetCount;
    } ## end while (log file lines)
    close($fh);
} ## end for (log files)

my $ips = [sort({$packetCountByIP->{$b}<=>$packetCountByIP->{$a}} keys(%$packetCountByIP))];
foreach my $ip (@$ips) {
    print("$ip,$packetCountByIP->{$ip}\n");
} ## end foreach (ips)
Comments