如何使用Linux脚本定位到日志文件中重复次数最多的行

1.背景

假如我们的生产环境遇到了日志异常增多的场景,这时候需要排查日志中具体哪块儿的请求打印的日志比较多,这里我们就需要用到 Linux 命令来排查问题。

2.命令

cat springboot-demo.jar.log | awk '{print $3}' | uniq -cd | sort -rn | head -n 10
  • cat 遍历当前日志文件所有内容;
  • awk '{print $3}' 筛选文件的第 3 列,这里我的第 3 列是 traceId;
  • uniq -cd 分组统计当前 traceId 重复出现的次数,并仅展示重复出现的行;
  • sort -rn 这里是 r 和 n,表示将内容以数字的形式进行降序排序;
  • head -n 10 展示出现次数排行前十的 traceId。

3.测试

springboot-demo.jar.log 内容如下:

2022-09-14 09:31:51.387 [69e7bad21f354071bc3e846c8dd2cd61,] INFO com.test.demo Test1...
2022-09-14 09:31:51.387 [69e7bad21f354071bc3e846c8dd2cd61,] INFO com.test.demo Test1...
2022-09-14 09:31:51.388 [69e7bad21f354071bc3e846c8dd2cd61,] INFO com.test.demo Test1...
2022-09-14 09:31:51.389 [69e7bad21f354071bc3e846c8dd2cd62,] INFO com.test.demo Test2...
2022-09-14 09:31:51.389 [69e7bad21f354071bc3e846c8dd2cd62,] INFO com.test.demo Test2...
2022-09-14 09:31:51.397 [69e7bad21f354071bc3e846c8dd2cd62,] INFO com.test.demo Test2...
2022-09-14 09:31:51.388 [69e7bad21f354071bc3e846c8dd2cd62,] INFO com.test.demo Test2...
2022-09-14 09:31:51.389 [69e7bad21f354071bc3e846c8dd2cd62,] INFO com.test.demo Test2...
2022-09-14 09:31:51.389 [69e7bad21f354071bc3e846c8dd2cd63,] INFO com.test.demo Test3...
2022-09-14 09:31:51.397 [69e7bad21f354071bc3e846c8dd2cd63,] INFO com.test.demo Test3...
2022-09-14 09:31:51.388 [69e7bad21f354071bc3e846c8dd2cd63,] INFO com.test.demo Test3...
2022-09-14 09:31:51.389 [69e7bad21f354071bc3e846c8dd2cd63,] INFO com.test.demo Test3...
2022-09-14 09:31:51.389 [69e7bad21f354071bc3e846c8dd2cd64,] INFO com.test.demo Test4...
2022-09-14 09:31:51.397 [69e7bad21f354071bc3e846c8dd2cd64,] INFO com.test.demo Test4...
2022-09-14 09:31:51.388 [69e7bad21f354071bc3e846c8dd2cd65,] INFO com.test.demo Test5...
2022-09-14 09:31:51.389 [69e7bad21f354071bc3e846c8dd2cd65,] INFO com.test.demo Test5...
2022-09-14 09:31:51.389 [69e7bad21f354071bc3e846c8dd2cd65,] INFO com.test.demo Test5...
2022-09-14 09:31:51.397 [69e7bad21f354071bc3e846c8dd2cd65,] INFO com.test.demo Test5...
2022-09-14 09:31:51.388 [69e7bad21f354071bc3e846c8dd2cd65,] INFO com.test.demo Test5...
2022-09-14 09:31:51.389 [69e7bad21f354071bc3e846c8dd2cd65,] INFO com.test.demo Test5...
2022-09-14 09:31:51.389 [69e7bad21f354071bc3e846c8dd2cd65,] INFO com.test.demo Test5...
2022-09-14 09:31:51.397 [69e7bad21f354071bc3e846c8dd2cd65,] INFO com.test.demo Test5...

执行结果:
如何使用Linux脚本定位到日志文件中重复次数最多的行_第1张图片

整理完毕,完结撒花~

你可能感兴趣的:(Linux杂谈,linux,运维,服务器)