shell统计日志,求每五分钟访问量的写法
日志格式如下:访问IP、时间、返回状态码
XX.XX.XX.XX - - [09/Sep/2015:10:30:00 +0800] 200
XX.XX.XX.XX - - [09/Sep/2015:10:34:00 +0800] 206
XX.XX.XX.XX - - [09/Sep/2015:10:37:00 +0800] 302
XX.XX.XX.XX - - [09/Sep/2015:10:32:00 +0800] 303
XX.XX.XX.XX - - [09/Sep/2015:11:30:00 +0800] 200
XX.XX.XX.XX - - [09/Sep/2015:11:39:00 +0800] 200
XX.XX.XX.XX - - [09/Sep/2015:12:29:00 +0800] 200
XX.XX.XX.XX - - [09/Sep/2015:12:30:00 +0800] 200
需求如下:
统计每五分钟的请求数,没有结果的输出为0(或者不输出都可以),如下格式:
10:00-10:04 0
10:05-10:09 0
- [root@test /tmp]$ awk -F: '{a[$2":"($3-$3%5)]++}END{for(i in a){split(i,t);print i,t[1]":"t[2]+4,a[i] | "sort -t: -k1n -k2n"}}' a
- 10:30 10:34 3
- 10:35 10:39 1
- 11:30 11:34 1
- 11:35 11:39 1
- 12:25 12:29 1
- 12:30 12:34 1
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
linux命令 对日志文件的IP出现的次数进行统计 并显示次数最多的前六名 解决方法:
grep -i -o -E "([0-9]{1,3}\.){3}[0-9]{1,3}" test1.txt | sort -n | uniq -c | sort -n -r | head -6
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
awk统计访问日志前10的ip 日志格式如下:
2013-10-2910:26:09,INFO, send toEmail=wuas6662@163.com,templateId=23,titleId=11,type=3,sendEmail=noreply
+renrenzhichang@mail.xiaozhao.com,ip=10.3.22.134,mailType=4,emailId=526f1bd8c8f2a90213662a67
shell命令如下:
cat mail-2013-10-28.log | awk -F ',' '{print $8}' | sort | uniq -c | sort -k1nr | head -10
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
awk 统计文件中满足特定条件的行数
例 文件tt.txt
sdfasf,I,55,56,asdfadf223
sdfasf,I,55,56,asdfadf2230k
1313,I,55,56,asdfad
xvxzv,I,55,56,asdf
adfa,a,d,afasd
vafasf,fff,aw,aaa
fasf,a,55,56,asdf
asdcc,I,55,fasdf,33
asdfasdf,I,fa,56,adsf
统计已逗号为分割的第二列为“I”,第三列为“55”,第四列为“56” 的行数,并输出已第一列为
的各行及对应出现次数 到文本sum.txt
awk 'BEGIN{FS=",";}{if($2=="I" && $3=="55" && $4=='56') a[$1]++} END{for (i in a) print i,a[i];}' tt.txt >sum.txt
或统计./diraa 目录下各个文件满足条件的
awk 'BEGIN{FS=",";}{if($2=="I" && $3=="55" && $4=='56') a[$1]++} END{for (i in a) print i,a[i];}' ./diraa/* >sum.txt