Mar 13 2008

perl进行单词计数,ip统计

Category: 技术ssmax @ 13:48:18

今天mima的tomcat又挂掉了,写一段够短的程序分析一下独立ip数量。。。

本来用sed写,写到一半好像写不下去了。。。

cat access_log | sed “s/ – – .*//”  |sort |????

然后用perl写了一个

perl countip.pl access_log |head

countip.pl

#!/usr/bin/perl
while(<>){
                chomp();
                $_ =~ s/ – – .+//g;
                $count{$_}++;
}
foreach $word (sort by_count keys %count){
        print “$word occurs $count{$word} times\n”;
}
sub by_count {
        $count{$b} <=> $count{$a}
}

sort使用了一个子例程作参数来定义排序规则

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.