#還是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 ./php.sh:
real 0m2.507s
user 0m2.384s
sys 0m0.105s