0612 grep查找关键字所在行以及其上下几行

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/weixin_30531261/article/details/80672012

百度到的信息

百度一下,各种博客,都是这么写:

linux系统中,利用grep打印匹配的上下几行

如果在只是想匹配模式的上下几行,grep可以实现。

$grep -5 ‘parttern’ inputfile //打印匹配行的前后5行

$grep -C 5 ‘parttern’ inputfile //打印匹配行的前后5行

$grep -A 5 ‘parttern’ inputfile //打印匹配行的后5行

$grep -B 5 ‘parttern’ inputfile //打印匹配行的前5行

可是我测试的时候,发现不行:

[liweizhi@bogon /]$ grep -C5 '异常:java.lang.NullPointerException' /home/tomcat/LogsYunnanEtcApiService
[liweizhi@bogon /]$ grep -5 '异常:java.lang.NullPointerException' /home/tomcat/LogsYunnanEtcApiService
[liweizhi@bogon /]$ grep -B 5 '异常:java.lang.NullPointerException' /home/tomcat/LogsYunnanEtcApiService
[liweizhi@bogon /]$ grep -5 '异常:java.lang.NullPointerException' /home/tomcat/LogsYunnanEtcApiService
[liweizhi@bogon /]$ grep -B 5 '异常:java.lang.NullPointerException' /home/tomcat/LogsYunnanEtcApiService
[liweizhi@bogon /]$ grep -C 5 '异常:java.lang.NullPointerException' /home/tomcat/LogsYunnanEtcApiService

-r可以显示关键字所在行

-r是可以的,但是只能显示符合的那一行:

[liweizhi@bogon /]$ grep -r '异常:java.lang.NullPointerException' /home/tomcat/LogsYunnanEtcApiService
/home/tomcat/LogsYunnanEtcApiService/logbackOutFile.log:********【仪电9001卡操作】====****====异常:java.lang.NullPointerException]
/home/tomcat/LogsYunnanEtcApiService/logbackOutFile.log:********【仪电9001卡操作】====****====异常:java.lang.NullPointerException]
/home/tomcat/LogsYunnanEtcApiService/logbackOutFile.log:********【仪电9001卡操作】====****====异常:java.lang.NullPointerException]
/home/tomcat/LogsYunnanEtcApiService/logbackOutFile.log:********【仪电9001卡操作】====****====异常:java.lang.NullPointerException]
/home/tomcat/LogsYunnanEtcApiService/logbackOutFile.log:********【仪电9001卡操作】====****====异常:java.lang.NullPointerException]
/home/tomcat/LogsYunnanEtcApiService/logbackOutFile.log:********【仪电9001卡操作】====****====异常:java.lang.NullPointerException]
/home/tomcat/LogsYunnanEtcApiService/logbackOutFile.log:********【仪电9001卡操作】====****====异常:java.lang.NullPointerException]
/home/tomcat/LogsYunnanEtcApiService/logbackOutFile.2018-06-11.0.log:********【仪电9001卡操作】====****====异常:java.lang.NullPointerException]
/home/tomcat/LogsYunnanEtcApiService/logbackOutFile.2018-06-11.0.log:********【仪电9001卡操作】====****====异常:java.lang.NullPointerException]
/home/tomcat/LogsYunnanEtcApiService/logbackOutFile.2018-06-11.0.log:********【仪电9001卡操作】====****====异常:java.lang.NullPointerException]
/home/tomcat/LogsYunnanEtcApiService/logbackOutFile.2018-06-11.0.log:********【仪电9001卡操作】====****====异常:java.lang.NullPointerException]
/home/tomcat/LogsYunnanEtcApiService/logbackOutFile.2018-06-11.0.log:********【仪电9001卡操作】====****====异常:java.lang.NullPointerException]
/home/tomcat/LogsYunnanEtcApiService/logbackOutFile.2018-06-11.0.log:********【仪电9001卡操作】====****====异常:java.lang.NullPointerException]
/home/tomcat/LogsYunnanEtcApiService/logbackOutFile.2018-06-11.0.log:********【仪电9001卡操作】====****====异常:java.lang.NullPointerException]

-r-B组合使用才可以

后来灵机一动,是不是-r是必备的,然后再加上-B 5这样呢?

试了一下,果然是这样:

扫描二维码关注公众号,回复: 3336124 查看本文章
[liweizhi@bogon /]$ grep -r -B 1 '异常:java.lang.NullPointerException' /home/tomcat/LogsYunnanEtcApiService

/home/tomcat/LogsYunnanEtcApiService/logbackOutFile.log-********【仪电9001卡操作】入参:com.uroad.etc.dto.StoreInput@2deac554[useruuid=d86a732d-2da6-11e8-add8-70106fadxxxx,orderNo=20180611000000xxxx,operationSequence=0101,instructions=803A010800A400000xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx,reportResults=<null>,useruuid=<null>,source=1]
/home/tomcat/LogsYunnanEtcApiService/logbackOutFile.log:********【仪电9001卡操作】====****====异常:java.lang.NullPointerException]
--
/home/tomcat/LogsYunnanEtcApiService/logbackOutFile.log-********【仪电9001卡操作】入参:com.uroad.etc.dto.StoreInput@2fbe1a1[useruuid=d86a732d-2da6-11e8-add8-70106fadxxxx,orderNo=20180611000000xxxx,operationSequence=0101,instructions=803A010800A400000xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx,reportResults=<null>,useruuid=<null>,source=1]
/home/tomcat/LogsYunnanEtcApiService/logbackOutFile.log:********【仪电9001卡操作】====****====异常:java.lang.NullPointerException]
--
/home/tomcat/LogsYunnanEtcApiService/logbackOutFile.log-********【仪电9001卡操作】入参:com.uroad.etc.dto.StoreInput@4364a874[useruuid=d86a732d-2da6-11e8-add8-70106fadxxxx,orderNo=20180611000000xxxx,operationSequence=0101,instructions=803A010800A400000xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx,reportResults=<null>,useruuid=<null>,source=1]
/home/tomcat/LogsYunnanEtcApiService/logbackOutFile.log:********【仪电9001卡操作】====****====异常:java.lang.NullPointerException]
--

总结

实践出真理啊!!!

之所以学这个操作,是为了日后在命令行直接查看日志,通过grep加关键字可以直接提取出符合条件的日志记录,而且使用grep命令可以同时对多个日志文件进行搜索。如果使用notepadd++工具查看,则只能对一个文件进行查看,而且无法直接过滤出符合条件的日志记录。

猜你喜欢

转载自blog.csdn.net/weixin_30531261/article/details/80672012