#!/usr/bin/perl
my $timestamp;
my $file_name;
my $listHost=false;
if ($#ARGV<2 || $#ARGV > 4){
print "Usage: -t 'time rexge' [-h] filename\n"
}else{
while ($ARGV = shift) {
if ($ARGV =~ m/-t/i) {
$timestamp = shift;
} elsif ($ARGV =~ /-h/i) {
$listHost=true;
} else{
$file_name = $ARGV;
}
}
}
my %res ;
my %tol;
open FILE_HD, "<", "$file_name";
while (defined($line = <FILE_HD>)) {
if ($line =~ /^($timestamp)[:print:]*/) {
my $time = $1 ;
$tol{$time}++;
if ( $line =~ /\(HOST=(\d+\.\d+\.\d+\.\d+)\)/ ) {
my $host_client = $1 ;
$res{$time}{$host_client}++ ;
}
}
}
my @res_key =sort by_liter keys %res;
foreach $loop (@res_key) {
if ( $loop == '' )
{ print "No time.\n";}
else
{ print "$loop => TOTAL:$tol{$loop}\n" ;}
if($listHost =~ /true/){
my $host_res = $res{$loop} ;
while ( ($key, $value) = each %$host_res ) {
print "\t$key \t=>\t $value\n";
}
}
}
sub by_liter {
$a cmp $b ; # ASCIIbetically by name
}
样例:
./1.pl -h -t '24-MAY-2016 16:[4-5]+' test.log
24-MAY-2016 16:4 => TOTAL:218
10.11.108.30 => 1
10.37.232.51 => 4
10.33.241.30 => 19
10.21.0.120 => 3
10.33.241.171 => 13
10.11.100.100 => 37
10.11.100.191 => 10
10.21.0.116 => 1
10.33.31.146 => 8
10.33.241.28 => 21
10.33.241.163 => 6