问题描述:服务中,对于几个G甚至几十个G的日志中查找某些报错信息,使用vim或者cat都不是很明智的选择。
分析:使用grep命令,加合理的参数查看分析日志
解决:
Context control:
-B, --before-context=NUM print NUM lines of leading context
-A, --after-context=NUM print NUM lines of trailing context
-C, --context=NUM print NUM lines of output context
说明:
-A 查看关键字后面指定行的的信息
例:grep -A 5 connectionError mycat.log 查看mycat.log中connectionError后面5行的内容
-B 查看关键字前面指定行的信息
例:grep -B 5 connectionError mycat.log 查看mycat.log中connectionError前面5行的内容
-C 查看关键字前后指定行的的信息
例:grep -C 5 connectionError mycat.log 查看mycat.log中connectionError前后5行的内容
grep -A 5 connectionError mycat.log
grep -B 5 connectionError mycat.log
grep -C 5 connectionError mycat.log
实际使用过程中遇到的小报错:查询完返回:
Binary file xxx.log matches
解决:加-a参数
例:grep -a -C 5 connectionError mycat.log