Re: sorting a file

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]



> I need to find out how many times an IP address appears in a file -  
> the
> IP is the first field in the access log string so what would be the  
> best
> way to sort this file and count how many times each IP address  
> appears ?

I always solve a problem like this with a small Perl script.  It can  
probably be done in awk as well, but I prefer Perl.  Try something  
like this:

=============== cut here ===============
#!/usr/bin/perl
use warnings;
use strict;

my $log = shift || "/var/log/httpd/access_log";
my %hash;

open(LOG, $log) || die "Can't open $log for reading ($!)\n";
while(<LOG>)
{
     next unless (/(^\d+\.\d+\.\d+\.\d+)/);
     $hash{$1}++;
}
close(LOG);

foreach (sort keys %hash)
{
     printf "%15s: %d occurances\n", $_, $hash{$_};
}
exit(0);
=============== cut here ===============

The script looks in either the file passed as the first parameter, or  
in the absence of that in /var/log/httpd/access_log.

Alfred

_______________________________________________
CentOS mailing list
CentOS@xxxxxxxxxx
http://lists.centos.org/mailman/listinfo/centos

[Index of Archives]     [CentOS]     [CentOS Announce]     [CentOS Development]     [CentOS ARM Devel]     [CentOS Docs]     [CentOS Virtualization]     [Carrier Grade Linux]     [Linux Media]     [Asterisk]     [DCCP]     [Netdev]     [Xorg]     [Linux USB]
  Powered by Linux