#还是php小脚本,进度缓慢,好几个功能不会写,下面是我今天写的,全是用来统计nginx日志有哪些ip地址访问的,(写的太烂了,哎)

方法1:

#这个还统计了每个IP访问了多少次

#!/usr/bin/php
<?php
$Con = array();
$Count=`awk '{print $1}' access.log | sort | uniq -c | awk '{print $1}'`;
$Count=split("\n",$Count);
$i=0;
$File_ = fopen("access.log","r");
        while(!feof($File_)){
                $Line = fgets($File_);
                $NEW_Line = preg_replace("/ .*/","",$Line);
                $Con[$NEW_Line]=1;
                }
                fclose($File_);
        foreach($Con as $Key=>$Value){
                $Key=trim($Key);
                echo "$Key\t\t$Count[$i]\n";
                $i++;
                }
?>

方法2:

#!/usr/bin/php
<?php
$array=array();
$File=split("( -)|\n",file_get_contents("qq"));
for($i=0;$i<count($File);$i+=3){
        $array[$File[$i]]=1;
        }
foreach($array as $key =>$value){
        echo "$key\n";
}
?>
 

方法3:

#!/usr/bin/php
<?php
$File=split("( -)|\n",file_get_contents("qq"));
$pattern="/^\d/";
foreach( array_unique($File) as $value){
        if(preg_match($pattern,$value)){
        echo "$value\n";
        }
}
?>

#方法2和方法3,只是统计有哪些IP访问,而且当数据10W行的时候,执行缓慢,处于蜗牛的状态

#方法1的执行速度也比较慢

time awk '{print $1}' <access.log |sort  | uniq -c 时间为:

real    0m2.153s
user    0m2.015s
sys     0m0.100s
 

方法1时间为 time ./:

real    0m2.507s


user    0m2.384s


sys     0m0.105s