10.19. Solution: Analyze Apache log fileExample 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";
}
|
Follow me: