Linux grep命令
目录
- 一. 前期准备
- 二. 配置项
- 2.1 -e 配置项
- 2.2 -h 配置项
- 三. 正则表达式
- 3.1 {} 或查询
- 3.2 文件路径和查询关键词中均包含正则表达式
- 四. zgrep
一. 前期准备
✅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 配置项
- 先查询ExecTime的值不为空白的数据,
-h
可以只匹配结果不匹配文件所在路径 - 然后使用
grep -e "^\S"
过滤掉开头为空白的数据 - 然后使用
awk '{print $1}'
打印第一列
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= 开头后跟非空白字符的部分。
# "^\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