假设访问日志格式如下,我们想统计cancel这个服务的qps:
[2017-04-25 15:31:11] service /ser1/cancel - - 175.57.2.60 77 94 0 0 -
[2017-04-25 15:31:11] service /ser1/cancel - - 175.57.2.63 77 94 0 0 -
[2017-04-25 15:31:11] service /ser1/cancel - - 175.57.2.64 77 94 0 0 -
[2017-04-25 15:31:11] service /ser1/cancel - - 175.57.2.65 77 94 0 0 -
[2017-04-25 15:31:11] service /ser1/cancel - - 175.57.2.66 77 94 0 0 -
[2017-04-25 15:31:11] service /ser1/cancel - - 175.57.2.67 77 94 0 0 -
[2017-04-25 15:31:11] service /ser1/cancel - - 175.57.2.68 77 94 0 0 -
[2017-04-25 15:31:13] service /ser1/cancel - - 175.57.2.69 77 94 0 0 -
[2017-04-25 15:31:15] service /ser1/cancel - - 175.57.2.69 77 94 0 0 -
[2017-04-25 16:31:11] service /ser1/delete - - 175.57.2.67 77 94 0 0 -
命令行为,很多教程使用的是{print $2},这样会取出右边的内容。
cat qps.txt | grep cancel | awk -F ']' '{print $1}' | cut -d ":" -f 3 | uniq -c
-F : awk脚本编辑器中设定分隔符awk -F ']' '{print $1}' ,上述命令取出时间[2017-04-25 15:31:11。
cut -d ':' -f 3 取出按分号分割后的第三列即取出秒。
uniq -c: 去重取计数