通过此语句可以查询一个文件或者当前目录下所有文件中包含exception和error的文件
grep -E -i "((exception)|(error))" *

1.查找文件中是否存在指定的信息:

grep <查找信息> <文件名>

-bash-4.1$ grep exception error-2015-12-29.log
17:49:13.869 [http-/192.168.0.80:8080-12] ERROR c.e.p.f.w.i.SystemHanlderExceptionResolver[37] - system has exception
-bash-4.1$ vi error-2015-12-29.log
-bash-4.1$

2.在多个文件中查找:
grep <查找信息> <文件名> <文件名> ....
-bash-4.1$ grep ERROR error-2015-12-29.log error-2015-12-22.log
error-2015-12-29.log:17:49:13.869 [http-/192.168.0.80:8080-12] ERROR c.e.p.f.w.i.SystemHanlderExceptionResolver[37] - system has exception
error-2015-12-22.log:10:05:31.824 [http-/192.168.0.80:8080-10] ERROR c.e.p.f.fastdfs.FastDFSClientImpl[97] - recv package size -1 != 10
error-2015-12-22.log:15:54:26.458 [http-/192.168.0.80:8080-6] ERROR c.e.p.f.fastdfs.FastDFSClientImpl[97] - recv package size -1 != 10
-bash-4.1$

3.使用 -l 查找包含指定信息的文件名
grep -l <查找信息> <文件名> <文件名> ....
-bash-4.1$ grep -l ERROR error-2015-12-29.log error-2015-12-22.log
error-2015-12-29.log
error-2015-12-22.log

4.使用 -n 查找包含指定信息的文件名和行号

grep -l <查找信息> <文件名> <文件名> ....

-bash-4.1$ grep -n ERROR error-2015-12-29.log error-2015-12-22.log
error-2015-12-29.log:1:17:49:13.869 [http-/192.168.0.80:8080-12] ERROR c.e.p.f.w.i.SystemHanlderExceptionResolver[37] - system has exception
error-2015-12-22.log:1:10:05:31.824 [http-/192.168.0.80:8080-10] ERROR c.e.p.f.fastdfs.FastDFSClientImpl[97] - recv package size -1 != 10
error-2015-12-22.log:2:15:54:26.458 [http-/192.168.0.80:8080-6] ERROR c.e.p.f.fastdfs.FastDFSClientImpl[97] - recv package size -1 != 10

5.使用 -v 查找不包含包含指定信息的文件名和行号
grep -V <查找信息> <文件名> <文件名> ....
-bash-4.1$ grep -v ERROR error-2015-12-29.log error-2015-12-22.log
error-2015-12-29.log:com.alibaba.dubbo.rpc.RpcException: Failed to invoke the method getLinkEffectivenessByEmail in the service com.exiao.platform.core.supplier.service.LinkEffectivenessService. Tried 1 times of the providers [192.168.0.91:20880] (1/1) from the registry zk1.exiao.me:2181 on the consumer 192.168.0.80 using the dubbo version 2.5.3. Last error is: Invoke remote method timeout. method: getLinkEffectivenessByEmail, provider: dubbo://192.168.0.91:20880/com.exiao.platform.core.supplier.service.LinkEffectivenessService?anyhost=true&application=api-supplier-dubbo&check=false&default.check=false&default.retries=0&default.timeout=5000&dubbo=2.5.3&interface=com.exiao.platform.core.supplier.service.LinkEffectivenessService&methods=update,delete,getLinkEffectivenessByEmail,getLinkEffectivenessByEmailCode,create,findList,getByPK&pid=24014&revision=0.0.1-SNAPSHOT&side=consumer×tamp=1450841202565, cause: Waiting server-side response timeout by scan timer. start time: 2015-12-29 17:49:08.844, end time: 2015-12-29 17:49:13.866, client elapsed: 0 ms, server elapsed: 5022 ms, timeout: 5000 ms, request: Request [id=2948, version=2.0.0, twoway=true, event=false, broken=false, data=RpcInvocation [methodName=getLinkEffectivenessByEmail, parameterTypes=[class com.exiao.platform.core.supplier.model.LinkEffectivenessVO], arguments=[LinkEffectivenessVO [url=null, email=yixiaoqatest6@sina.com, effectiveTime=null,used=0,code=null,function=validate]], attachments={path=com.exiao.platform.core.supplier.service.LinkEffectivenessService, interface=com.exiao.platform.core.supplier.service.LinkEffectivenessService, timeout=5000, version=0.0.0}]], channel: /192.168.0.80:48178 -> /192.168.0.91:20880
error-2015-12-29.log: at com.alibaba.dubbo.rpc.cluster.support.FailoverClusterInvoker.doInvoke(FailoverClusterInvoker.java:101) ~[dubbo-2.5.3.jar:2.5.3]
error-2015-12-29.log: at com.alibaba.dubbo.rpc.cluster.support.AbstractClusterInvoker.invoke(AbstractClusterInvoker.java:227) ~[dubbo-2.5.3.jar:2.5.3]
error-2015-12-29.log: at com.alibaba.dubbo.rpc.cluster.support.wrapper.MockClusterInvoker.invoke(MockClusterInvoker.java:72) ~[dubbo-2.5.3.jar:2.5.3]
error-2015-12-29.log: at com.alibaba.dubbo.rpc.proxy.InvokerInvocationHandler.invoke(InvokerInvocationHandler.java:52) ~[dubbo-2.5.3.jar:2.5.3]
error-2015-12-29.log: at com.alibaba.dubbo.common.bytecode.proxy4.getLinkEffectivenessByEmail(proxy4.java) ~[na:2.5.3]
error-2015-12-29.log: at com.exiao.platform.api.supplier.controller.LinkEffectivenessController.reSending(LinkEffectivenessController.java:42) ~[classes:na]
error-2015-12-29.log: at sun.reflect.GeneratedMethodAccessor1051.invoke(Unknown Source) ~[na:na]
error-2015-12-29.log: at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.7.0_79]
error-2015-12-29.log: at java.lang.reflect.Method.invoke(Method.java:606) ~[na:1.7.0_79]

6 使用 -R 在目标路径下递归查找指定信息:
grep -V <查找信息> <目标路径>

-bash-4.1$ grep -r ERROR ./
./default-2015-12-29.log:17:49:13.869 [http-/192.168.0.80:8080-12] ERROR c.e.p.f.w.i.SystemHanlderExceptionResolver[37] - system has exception
./error-2015-12-22.log:10:05:31.824 [http-/192.168.0.80:8080-10] ERROR c.e.p.f.fastdfs.FastDFSClientImpl[97] - recv package size -1 != 10
./error-2015-12-22.log:15:54:26.458 [http-/192.168.0.80:8080-6] ERROR c.e.p.f.fastdfs.FastDFSClientImpl[97] - recv package size -1 != 10
./error-2015-12-24.log:09:57:24.202 [http-/192.168.0.80:8080-2] ERROR c.e.p.f.fastdfs.FastDFSClientImpl[97] - recv package size -1 != 10
./error-2015-12-23.log:10:08:06.604 [http-/192.168.0.80:8080-11] ERROR c.e.p.f.fastdfs.FastDFSClientImpl[97] - recv package size -1 != 10
./error-2015-12-29.log:17:49:13.869 [http-/192.168.0.80:8080-12] ERROR c.e.p.f.w.i.SystemHanlderExceptionResolver[37] - system has exception<span style="font-size:18px;"><strong>
</strong></span>

7 使用 -i 查找信息时忽略大小写:
grep -i <查找信息> <文件名> <文件名> ....

-bash-4.1$ grep -i error error-2015-12-29.log error-2015-12-22.log
error-2015-12-29.log:17:49:13.869 [http-/192.168.0.80:8080-12] ERROR c.e.p.f.w.i.SystemHanlderExceptionResolver[37] - system has exception<span style="font-size:18px;"><strong><span style="color:#ff0000;">
</span></strong></span>

8.使用 -e 同时查找多个信息:
grep -e <查找信息> -e <查找信息> ... <文件名> <文件名> ....

-bash-4.1$ grep -e "ERROR" -e "Exception" error-2015-12-29.log error-2015-12-22.log
error-2015-12-29.log:17:49:13.869 [http-/192.168.0.80:8080-12] ERROR c.e.p.f.w.i.SystemHanlderExceptionResolver[37] - system has exception
error-2015-12-29.log:com.alibaba.dubbo.rpc.RpcException: Failed to invoke the method getLinkEffectivenessByEmail in the service com.exiao.platform.core.supplier.service.LinkEffectivenessService. Tried 1 times of the providers [192.168.0.91:20880] (1/1) from the registry zk1.exiao.me:2181 on the consumer 192.168.0.80 using the dubbo version 2.5.3. Last error is: Invoke remote method timeout. method: getLinkEffectivenessByEmail, provider: dubbo://192.168.0.91:20880/com.exiao.platform.core.supplier.service.LinkEffectivenessService?anyhost=true&application=api-supplier-dubbo&check=false&default.check=false&default.retries=0&default.timeout=5000&dubbo=2.5.3&interface=com.exiao.platform.core.supplier.service.LinkEffectivenessService&methods=update,delete,getLinkEffectivenessByEmail,getLinkEffectivenessByEmailCode,create,findList,getByPK&pid=24014&revision=0.0.1-SNAPSHOT&side=consumer×tamp=1450841202565, cause: Waiting server-side response timeout by scan timer. start time: 2015-12-29 17:49:08.844, end time: 2015-12-29 17:49:13.866, client elapsed: 0 ms, server elapsed: 5022 ms, timeout: 5000 ms, request: Request [id=2948, version=2.0.0, twoway=true, event=false, broken=false, data=RpcInvocation [methodName=getLinkEffectivenessByEmail, parameterTypes=[class com.exiao.platform.core.supplier.model.LinkEffectivenessVO], arguments=[LinkEffectivenessVO [url=null, email=yixiaoqatest6@sina.com, effectiveTime=null,used=0,code=null,function=validate]], attachments={path=com.exiao.platform.core.supplier.service.LinkEffectivenessService, interface=com.exiao.platform.core.supplier.service.LinkEffectivenessService, timeout=5000, version=0.0.0}]], channel: /192.168.0.80:48178 -> /192.168.0.91:20880
error-2015-12-29.log:Caused by: com.alibaba.dubbo.remoting.TimeoutException: Waiting server-side response timeout by scan timer. start time: 2015-12-29 17:49:08.844, end time: 2015-12-29 17:49:13.866, client elapsed: 0 ms, server elapsed: 5022 ms, timeout: 5000 ms, request: Request [id=2948, version=2.0.0, twoway=true, event=false, broken=false, data=RpcInvocation [methodName=getLinkEffectivenessByEmail, parameterTypes=[class com.exiao.platform.core.supplier.model.LinkEffectivenessVO], arguments=[LinkEffectivenessVO [url=null, email=yixiaoqatest6@sina.com, effectiveTime=null,used=0,code=null,function=validate]], attachments={path=com.exiao.platform.core.supplier.service.LinkEffectivenessService, interface=com.exiao.platform.core.supplier.service.LinkEffectivenessService, timeout=5000, version=0.0.0}]], channel: /192.168.0.80:48178 -> /192.168.0.91:20880
error-2015-12-22.log:10:05:31.824 [http-/192.168.0.80:8080-10] ERROR c.e.p.f.fastdfs.FastDFSClientImpl[97] - recv package size -1 != 10
error-2015-12-22.log:15:54:26.458 [http-/192.168.0.80:8080-6] ERROR c.e.p.f.fastdfs.FastDFSClientImpl[97] - recv package size -1 != 10<span style="font-size:18px;"><strong>
</strong></span>

9 使用 -c 参数计算查找到的结果数量:
grep -c <查找信息> <文件名> <文件名> ....

-bash-4.1$ grep -c ERROR error-2015-12-29.log error-2015-12-22.log
error-2015-12-29.log:1
error-2015-12-22.log:2

其他一些用法:
使用管道将结果使用grep 过滤,输入包含指定信息的行

命令 | grep <匹配信息>

bash-4.1$ cat error-2015-12-22.log | grep ERROR
10:05:31.824 [http-/192.168.0.80:8080-10] ERROR c.e.p.f.fastdfs.FastDFSClientImpl[97] - recv package size -1 != 10
15:54:26.458 [http-/192.168.0.80:8080-6] ERROR c.e.p.f.fastdfs.FastDFSClientImpl[97] - recv package size -1 != 10<span style="font-size:18px;"><strong><span style="color:#ff0000;">
</span></strong></span>

还有其他一些参数如 -f , ^$,-A,-B,-C等 可自行查询用法.
————————————————
版权声明:本文为CSDN博主「wangxin0314」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/wangxin1982314/article/details/50464259

 

linux grep基本用法--九五小庞的更多相关文章

  1. linux grep的用法

    linux grep的用法<pre>[root@iZ23uewresmZ ~]# cat /home/ceshis.txtb124230 b034325 a081016 m7187998 ...

  2. LINUX系统新增及自动挂载硬盘-九五小庞

    Linux系统下,添加新硬盘后,自动挂载的方法   1,列出所有硬盘,找到需要挂载的硬盘,例如/dev/vdb.输入: fdisk -l   2,查看硬盘是不是已经被挂载.一个硬盘不能重复挂载,已经挂 ...

  3. WinMTR 网络测试工具-九五小庞

    WinMTR(建议优先使用) 百度下载工具 链接:https://pan.baidu.com/s/19ArKSTA2amsa4p6vHegDIQ 提取码:cy4y WinMTR是mtr工具在Windo ...

  4. VMware安装Centos7 -九五小庞

    VMware安装Centos7超详细过程(图文) https://blog.csdn.net/babyxue/article/details/80970526 安装centos7的时候 启动会提示Pl ...

  5. centos7制作U盘启动盘-九五小庞

    一.准备相关软件 1.8G以上U盘 2.UltraISO虚拟光驱(试用版即可)最新版 下载地址:https://cn.ultraiso.net/xiazai.html  点击下载试用 3.CentOS ...

  6. Oracle 11G R2安装说明 -九五小庞

    教程版本Oracle 11.2.0.1.0

  7. 什么是Nginx -九五小庞

  8. Oracle错误 ora-12514 解决方法-九五小庞

    成功连到数据库上之后,查看listener状态:lsnrctl status status READY 状态,需要由非归档转为归档模式,故操作如下: 1.关闭数据库shutdown immediate ...

  9. oracle创建/删除 用户,表空间-九五小庞

    以下红色标示的都是可以修改的字段 可以按照如下顺序来创建表空间,创建用户,以及删除表空间,删除用户 查看oracle数据库已有的表空间路径 select name from v$datafile; 1 ...

  10. ORA-01033错误解决方案-九五小庞

    Microsoft Windows [版本 6.3.9600](c) 2013 Microsoft Corporation.保留所有权利. C:\Users\Administrator>sqlp ...

随机推荐

  1. [GIT]辨析/区别: git reset HEAD 与 git reset --hard HEAD | 版本回撤

    1 场景1: 撤销到远程仓库或本地仓库的最新最近一次的正式版本 1.1 文由 时常有这样一种场景,不小心改动了部分文件,或修改了部分文件却发现无用,此时可能还没有git push,也可能push了:又 ...

  2. Golang一日一库之logrus

    前言 之前一篇文章介绍了 日志库zap https://www.cnblogs.com/zichliang/p/17311480.html 毋庸置疑,zap库无论是Golang在项目中 还是生产中都极 ...

  3. django使用多个数据库实现

    一.说明: 在开发 Django 项目的时候,很多时候都是使用一个数据库,即 settings 中只有 default 数据库,但是有一些项目确实也需要使用多个数据库,这样的项目,在数据库配置和使用的 ...

  4. pg序列的增删改查

    添加序列. CREATE SEQUENCE IF NOT EXISTS public.data_device_id_seq INCREMENT 1 START 1 MINVALUE 1 MAXVALU ...

  5. 2022-12-10:给你一个由小写字母组成的字符串 s ,和一个整数 k 如果满足下述条件,则可以将字符串 t 视作是 理想字符串 : t 是字符串 s 的一个子序列。 t 中每两个 相邻 字母在字

    2022-12-10:给你一个由小写字母组成的字符串 s ,和一个整数 k 如果满足下述条件,则可以将字符串 t 视作是 理想字符串 : t 是字符串 s 的一个子序列. t 中每两个 相邻 字母在字 ...

  6. 2022-11-06:给定平面上n个点,x和y坐标都是整数, 找出其中的一对点的距离,使得在这n个点的所有点对中,该距离为所有点对中最小的。 返回最短距离,精确到小数点后面4位。

    2022-11-06:给定平面上n个点,x和y坐标都是整数, 找出其中的一对点的距离,使得在这n个点的所有点对中,该距离为所有点对中最小的. 返回最短距离,精确到小数点后面4位. 答案2022-11- ...

  7. 2022-10-24:以下go语言代码输出什么?A:3 3;B:3 4;C:0 0;D:0 1。 package main func main() { m := make(map[int]int

    2022-10-24:以下go语言代码输出什么?A:3 3:B:3 4:C:0 0:D:0 1. package main func main() { m := make(map[int]int, 3 ...

  8. 2022-01-20: 矩形区域不超过 K 的最大数值和。 给你一个 m x n 的矩阵 matrix 和一个整数 k ,找出并返回矩阵内部矩形区域的不超过 k 的最大数值和。 题目数据保证总会存在一

    2022-01-20: 矩形区域不超过 K 的最大数值和. 给你一个 m x n 的矩阵 matrix 和一个整数 k ,找出并返回矩阵内部矩形区域的不超过 k 的最大数值和. 题目数据保证总会存在一 ...

  9. 2021-12-19:找到所有数组中消失的数字。 给你一个含 n 个整数的数组 nums ,其中 nums[i] 在区间 [1, n] 内。请你找出所有在 [1, n] 范围内但没有出现在 nums

    2021-12-19:找到所有数组中消失的数字. 给你一个含 n 个整数的数组 nums ,其中 nums[i] 在区间 [1, n] 内.请你找出所有在 [1, n] 范围内但没有出现在 nums ...

  10. Nacos必知必会:这些知识点你一定要掌握!

    前言 Nacos 是一个开源的服务发现.配置管理和服务治理平台,是阿里巴巴开源的一款产品. Nacos 可以帮助开发者更好地管理微服务架构中的服务注册.配置和发现等问题,提高系统的可靠性和可维护性. ...