一. 前期准备
✅TEST-2023-07-11.txt
MPLE0130 Exception 123 ExecTime=
MPLE0190 ExecTime=123
MPLE0150 TST 1234 ExecTime=454
MPLE0160 Exception 123 ExecTime=
MPLE0170 TST 1234 ExecTime=999
✅TEST-2023-09-11.txt
CCCE0130 Exception 123
MPLE0150 TST 1234
MPLE0160 Exception 123
CCCE0170 TST 1234
✅TEST-2023-10-11.txt
MPLE0130 Exception 123 ExecTime=
MPLE0190 ExecTime=123
MPLE0150 TST 1234 ExecTime=454
MPLE0160 Exception 123 ExecTime=
MPLE0170 TST 1234 ExecTime=999
二. 配置项
2.1 -e 配置项
⏹下面两种写法相同,查询的都是MPLE0130或CCCE0130
grep -a -e MPLE0130 -e CCCE0130 /home/fengyehong/jmw_work_space/TEST-2023-10-11.txt
grep -aE 'MPLE0130|CCCE0130' /home/fengyehong/jmw_work_space/TEST-2023-10-11.txt
[2023-11-12 18:27:49.427] grep -a -e MPLE0130 -e CCCE0130 /home/fengyehong/jmw_work_space/TEST-2023-10-11.txt
[2023-11-12 18:27:51.967] MPLE0130 Exception 123 ExecTime=
⏹查询TEST-2023-*-11.txt
文件夹中包含Exception
关键字的
grep -a -e Exception /home/fengyehong/jmw_work_space/TEST-2023-*-11.txt
[2023-11-12 18:27:51.970] fengyehong@ubuntu:~$ grep -a -e Exception /home/fengyehong/jmw_work_space/TEST-2023-*-11.txt
[2023-11-12 18:31:47.356] /home/fengyehong/jmw_work_space/TEST-2023-07-11.txt:MPLE0130 Exception 123 ExecTime=
[2023-11-12 18:31:47.363] /home/fengyehong/jmw_work_space/TEST-2023-07-11.txt:MPLE0160 Exception 123 ExecTime=
[2023-11-12 18:31:47.371] /home/fengyehong/jmw_work_space/TEST-2023-09-11.txt:CCCE0130 Exception 123
[2023-11-12 18:31:47.378] /home/fengyehong/jmw_work_space/TEST-2023-09-11.txt:MPLE0160 Exception 123
[2023-11-12 18:31:47.387] /home/fengyehong/jmw_work_space/TEST-2023-10-11.txt:MPLE0130 Exception 123 ExecTime=
[2023-11-12 18:31:47.394] /home/fengyehong/jmw_work_space/TEST-2023-10-11.txt:MPLE0160 Exception 123 ExecTime=
2.2 -h 配置项
grep -a -h -e "ExecTime=S*" /home/fengyehong/jmw_work_space/TEST-2023-{0[6-9],10}-11.txt | grep -e "^S" | awk '{print $1}'
[2023-11-12 18:54:27.074] fengyehong@ubuntu:~$ grep -a -h -e "ExecTime=S*" /home/fengyehong/jmw_work_space/TEST-2023-{0[6-9],10}-11.txt | grep -e "^S" | awk '{print $1}'
[2023-11-12 18:55:41.989] MPLE0130
[2023-11-12 18:55:41.990] MPLE0150
[2023-11-12 18:55:41.990] MPLE0160
[2023-11-12 18:55:41.990] MPLE0170
[2023-11-12 18:55:41.990] MPLE0130
[2023-11-12 18:55:41.990] MPLE0150
[2023-11-12 18:55:41.990] MPLE0160
[2023-11-12 18:55:41.990] MPLE0170
三. 正则表达式
3.1 {} 或查询
⏹{}
表示或,{0[6-9],10}
表示从06月到10月。
grep -a -e Exception /home/fengyehong/jmw_work_space/TEST-2023-{0[6-9],10}-11.txt
[2023-11-12 18:33:28.388] fengyehong@ubuntu:~$ grep -a -e Exception /home/fengyehong/jmw_work_space/TEST-2023-{0[6-9],10}-11.txt
[2023-11-12 18:33:30.750] /home/fengyehong/jmw_work_space/TEST-2023-07-11.txt:MPLE0130 Exception 123 ExecTime=
[2023-11-12 18:33:30.757] /home/fengyehong/jmw_work_space/TEST-2023-07-11.txt:MPLE0160 Exception 123 ExecTime=
[2023-11-12 18:33:30.765] /home/fengyehong/jmw_work_space/TEST-2023-09-11.txt:CCCE0130 Exception 123
[2023-11-12 18:33:30.772] /home/fengyehong/jmw_work_space/TEST-2023-09-11.txt:MPLE0160 Exception 123
[2023-11-12 18:33:30.780] /home/fengyehong/jmw_work_space/TEST-2023-10-11.txt:MPLE0130 Exception 123 ExecTime=
[2023-11-12 18:33:30.787] /home/fengyehong/jmw_work_space/TEST-2023-10-11.txt:MPLE0160 Exception 123 ExecTime=
⏹从两个文件名类似的文件中查询
# 从 20231104bvspl.log 或 20231104bvapp.log 中进行查询
grep -a Error ./log/.20231104bv{spl,app}.log*
3.2 文件路径和查询关键词中均包含正则表达式
# "^S" 表示匹配非空白字符开头的部分
# "ExecTime=S*" 匹配以 ExecTime= 开头后跟非空白字符的部分
grep -a -e "^S" -e "ExecTime=S*" /home/fengyehong/jmw_work_space/TEST-2023-{0[6-9],10}-11.txt
[2023-11-12 18:36:15.874] fengyehong@ubuntu:~$ grep -a -e "^S" -e "ExecTime=S*" /home/fengyehong/jmw_work_space/TEST-2023-{0[6-9],10}-11.txt
[2023-11-12 18:37:14.504] /home/fengyehong/jmw_work_space/TEST-2023-07-11.txt:MPLE0130 Exception 123 ExecTime=
[2023-11-12 18:37:14.521] /home/fengyehong/jmw_work_space/TEST-2023-07-11.txt: MPLE0190 ExecTime=123
[2023-11-12 18:37:14.535] /home/fengyehong/jmw_work_space/TEST-2023-07-11.txt:MPLE0150 TST 1234 ExecTime=454
[2023-11-12 18:37:14.553] /home/fengyehong/jmw_work_space/TEST-2023-07-11.txt:MPLE0160 Exception 123 ExecTime=
[2023-11-12 18:37:14.570] /home/fengyehong/jmw_work_space/TEST-2023-07-11.txt:MPLE0170 TST 1234 ExecTime=999
[2023-11-12 18:37:14.587] /home/fengyehong/jmw_work_space/TEST-2023-09-11.txt:CCCE0130 Exception 123
[2023-11-12 18:37:14.600] /home/fengyehong/jmw_work_space/TEST-2023-09-11.txt:MPLE0150 TST 1234
[2023-11-12 18:37:14.615] /home/fengyehong/jmw_work_space/TEST-2023-09-11.txt:MPLE0160 Exception 123
[2023-11-12 18:37:14.647] /home/fengyehong/jmw_work_space/TEST-2023-09-11.txt:CCCE0170 TST 1234
[2023-11-12 18:37:14.662] /home/fengyehong/jmw_work_space/TEST-2023-10-11.txt:MPLE0130 Exception 123 ExecTime=
[2023-11-12 18:37:14.679] /home/fengyehong/jmw_work_space/TEST-2023-10-11.txt: MPLE0190 ExecTime=123
[2023-11-12 18:37:14.694] /home/fengyehong/jmw_work_space/TEST-2023-10-11.txt:MPLE0150 TST 1234 ExecTime=454
[2023-11-12 18:37:14.712] /home/fengyehong/jmw_work_space/TEST-2023-10-11.txt:MPLE0160 Exception 123 ExecTime=
[2023-11-12 18:37:14.727] /home/fengyehong/jmw_work_space/TEST-2023-10-11.txt:MPLE0170 TST 1234 ExecTime=999
四. zgrep
# 统计 Desktop.zip 压缩包中 fengyehong 这个单词出现的次数
zgrep -a -e fengyehong /home/fengyehong/jmw_work_space/Desktop.zip | wc -l
原文地址:https://blog.csdn.net/feyehong/article/details/134626979
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如若转载,请注明出处:http://www.7code.cn/show_4683.html
如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱:suwngjj01@126.com进行投诉反馈,一经查实,立即删除!
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。