linuxjishu/13830.html target=_blank class=infotextkey>awk命令分析nginx日志文件:
awk 'BEGIN {FS=" - "} {if(index($5,"GET /index.do?snstoken=")>0 || index($5,"GET /index.do ")>0) a[$1]++} END{for (i in a) {print i,a[i]}}' access.log.2010-01-25 > statisticsaccess.log.2010-01-25
代码说明:
BEGIN分析行开始前做初始化工作
END分析行结束后做输出操作
a[$1]++对$1所代表的下标的值加1
index相当于java中indexOf操作,不过这里面找不到返回0,而不是-1
for (i in a) {print i,a[i]}循环将a的下标给i,循环打印
> statisticsaccess.log.2010-01-25以管道形式将结果写到文件中
另附,时间戳与日期转换
awk 'BEGIN{print strftime("%Y-%m-%d%H",1154761740)}' =2006080515
awk 'BEGIN {printf("%d/n",mktime(2006" "8" "5" "15" "09" "0))}' =1154761740