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
⏹下面两种写法相同,查询的都是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=
-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
⏹如下图所示的文本,我们想要匹配红框中的内容。
--context
配置项的缩写,在匹配行的上方和下方显示额外的文本行。-C 3
:显示匹配行的上下方的3行grep -a -C 3 -e "uuid=\S*" ./content.txt
--after-context
配置项的缩写,在匹配行的下方显示额外的文本行。-A 3
:显示匹配行的下方的3行。grep -a -A 3 -e "uuid=\S*" ./content.txt
--before-context
配置项的缩写,在匹配行的下方显示额外的文本行。-B 3
:显示匹配行的下方的3行。grep -a -B 3 -e "uuid=\S*" ./content.txt
⏹主要用来查询压缩包中的信息
# 统计 Desktop.zip 压缩包中 fengyehong 这个单词出现的次数
zgrep -a -e fengyehong /home/fengyehong/jmw_work_space/Desktop.zip | wc -l
file1.log
123 aaa 你好
345 bbb 我好
345 ccc 大家好
124 ddd 世界好
111 rrr 哈哈哈
file2.log
123 mmm 你好mmm
366 nnn 我好nnn
377 fff 大家好fff
124 uuu 世界好uuu
111 iii 哈哈哈iii
分析
两个文本文件格式相同,都是3列,第一例都是数字编号。可以通过打开这两个文件然后获取出开头列,然后排序之后再去重。
转换为linux命令之后就是
-o
配置项只输出匹配到的内容^\S*
匹配开头不为空的内容sort
用来排序uniq
用来去重cat file1.log file2.log | egrep -o -a "^\S*" | sort | uniq
111
123
124
345
366
377
如果我们想查看每个数字编码出现的次数的话,给uniq
命令加上-c
配置项即可。
其中第一列是数字编码出现的次数。
cat file1.log file2.log | egrep -o -a "^\S*" | sort | uniq -c
2 111
1 123
2 124
2 345
1 366
1 377
⏹{}
表示或,{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*
⏹路径和查询关键词中均包含正则表达式
^\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
file3.log
110_A6 你好
120_A7 世界
110_G3 hello
110_G4 world
120_A7 こんにちは
140_G3 この世界
150_G4 uuuuuu
110_A6
120_A7
110_G3
110_G4
120_A7
140_G3
150_G4
⏹查询以110_
开头,内容包括A6,G3,G4的内容
egrep -a "^110_(A6|G3|G4)" file3.log
110_A6 你好
110_G3 hello
110_G4 world
110_G3
110_G4
⏹查询以110_
开头,内容包括A6,G3,G4的内容,并以此为结尾的内容
egrep -a "^110_(A6|G3|G4)$" file3.log
110_G3
110_G4
⏹查询以非空开头,内容包括A6,G3,G4的内容,并以此为结尾的内容
egrep -a "^\S*_(A6|G3|G4)$" file3.log
110_G3
110_G4
140_G3
150_G4
⏹查询以非空开头,内容包括A6,G3,G4的内容
egrep -a "^\S*_(A6|G3|G4)" file3.log
110_A6 你好
110_G3 hello
110_G4 world
140_G3 この世界
150_G4 uuuuuu
110_G3
110_G4
140_G3
150_G4
有如下日志file4.log
2323 SEQIN mmm isuuePayId=5768awe uuid=woenoo; jmw_state=success method=paypay info=ppp
2323 SEQOUT COST=45726
2345 SEQIN mmm isuuePayId=34895ry uuid=;ljkler jmw_state=faile method=alipay info=ddd
2345 SEQOUT COST=34855
需求,抽取出SEQIN类日志中的第一个字段,isuuePayId 和 jmw_state 字段,让其在一行显示
⏹第一步,抽取SEQIN类日志,去除掉 SEQOUT 日志
grep -a "SEQIN" file4.log
2323 SEQIN mmm isuuePayId=5768awe uuid=woenoo; jmw_state=success method=paypay info=ppp
2345 SEQIN mmm isuuePayId=34895ry uuid=;ljkler jmw_state=faile method=alipay info=ddd
⏹第二步,获取出SEQIN类日志中的第一个字段,isuuePayId 和 jmw_state 字段
grep -a "SEQIN" file4.log | egrep -o -a -e "^\S*" -e "isuuePayId=\S*" -e "jmw_state=\S*"
2323
isuuePayId=5768awe
jmw_state=success
2345
isuuePayId=34895ry
jmw_state=faile
⏹第三步,让isuuePayId和jmw_state到一行上显示
grep -a "SEQIN" file4.log
# 进一步过滤
| egrep -o -a -e "^\S*" -e "isuuePayId=\S*" -e "jmw_state=\S*"
# 替换指定字段的换行符
| sed ':loop; N; $!b loop; ;s/\n\([ij]\)/ \1/g'
2323 isuuePayId=5768awe jmw_state=success
2345 isuuePayId=34895ry jmw_state=faile
[^ ]*
和\S*
20240109_xx.txt
123 name=jiafeitian transactionId=110120 resultCode=0
456 name=zhangsan transactionId= resultCode=1
789 name=lisi transactionId=
111 name=wangwu transactionId=890657 resultCode=0
需求:获取出所有的transactionId字段
⏹使用[^ ]*
来获取
grep -o "transactionId=[^ ]*" ./20240109_xx.txt
transactionId=110120
transactionId=
transactionId=
transactionId=890657
⏹使用\S*
来获取
grep -o "transactionId=\S*" ./20240109_xx.txt
transactionId=110120
transactionId=
transactionId=
transactionId=890657