10.19. Solution: Analyze Apache log file

Example 10-8. examples/hashes/apache_log_hosts_hash.pl

#!/usr/bin/perl
use strict;
use warnings;

my $file = shift or die "Usage: $0 FILENAME (examples/files/apache_access.log)\n";
open my $fh, '<', $file or die $!;

my %count;
while (my $line = <$fh>) {
    chomp $line;
    my $length = index ($line, " ");
    my $ip = substr($line, 0, $length);
    $count{$ip}++;   
}

foreach my $ip (keys %count) {
    print "$ip   $count{$ip}\n";
}

If you are interested in on-site trainings by the author, please contact me directly.

Online courses:

Would you like to get
updated when I publish
the next article?

Follow me:

Google Plus Twitter RSS feed