使用awk命令筛选日志文件中执行时间超过200ms的SQL日志信息

以后再面试的时候,如果有面试官再问我这个问题:“请说一下你常用的linux命令”,那么awk就可以作为答案之一了。
日志中要筛选的具体日志信息如下,其中包含了mapper以及SQL执行的时间

2019-12-20 15:05:14.133 29958268 [http-nio-8080-exec-64] INFO  com.simple.page.MyBatisSqlCostInterceptor.intercept[46] - []The SQL request method is : [com.simple.module.infopanel.mapper.InfoPanelMapper.getSimStatQuery],execute the SQL cost 457 ms

现在就是要把日志所有执行时间超过200ms的筛选出来。
具体的脚本如下:

cat service.log | awk '/execute the SQL cost ([0-9]+) ms/' | awk '{costms=gensub(/.+execute the SQL cost ([0-9]*) ms/,"\\1",1);if(costms+0 > 200) print NR, $0}' >> out.log

简单解释一下:
cat service.log读取日志文件,使用管道命令把读取的内容作为awk的输入,
'/execute the SQL cost ([0-9]+) ms/'正则,目的是筛选包含SQL执行时间的日志,
gensub(/.+execute the SQL cost ([0-9]*) ms/,"\\1",1)awk的gensub函数,"\\1"是正则匹配的第一个括号内的内容。
print NR, $0打印行号和当前行。

发布了28 篇原创文章 · 获赞 1 · 访问量 1865

猜你喜欢

转载自blog.csdn.net/m0_46130323/article/details/103878572