Linux grep命令


一. 前期准备

📚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 配置项 (或查询)

⏹下面两种写法相同,查询的都是MPLE0130CCCE0130

# 通过 -e 进行或查询
grep -a -e MPLE0130 -e CCCE0130 /home/fengyehong/jmw_work_space/TEST-2023-10-11.txt
# 通过 -E 开启扩展正则表达式,进行或查询
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
fengyehong@ubuntu:~$ grep -a -e Exception /home/fengyehong/jmw_work_space/TEST-2023-*-11.txt
/home/fengyehong/jmw_work_space/TEST-2023-07-11.txt:MPLE0130 Exception 123 ExecTime=
/home/fengyehong/jmw_work_space/TEST-2023-07-11.txt:MPLE0160 Exception 123 ExecTime=
/home/fengyehong/jmw_work_space/TEST-2023-09-11.txt:CCCE0130 Exception 123
/home/fengyehong/jmw_work_space/TEST-2023-09-11.txt:MPLE0160 Exception 123
/home/fengyehong/jmw_work_space/TEST-2023-10-11.txt:MPLE0130 Exception 123 ExecTime=
/home/fengyehong/jmw_work_space/TEST-2023-10-11.txt:MPLE0160 Exception 123 ExecTime=

2.2 -E 配置项(拓展正则表达式)

  • 使用 -E 选项可以使 grep 支持更多的正则表达式语法,包括使用 +?| 等特殊符号进行模式匹配。
  • 获取08:05 至 08:0708:13 至 08:15之间的时间范围。
grep -aE "08:(0[5-7]|1[3-5]):[0-9][0-9]" ./20240331_test_app.log
  • 获取08:10 至 08:1908:30 至 08:3908:50 至 08:59之间的时间范围。
grep -aE "08:(1|3|5)[0-9]:[0-9][0-9]" ./20240331_test_app.log

2.3 -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}'
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}'
MPLE0130
MPLE0150
MPLE0160
MPLE0170
MPLE0130
MPLE0150
MPLE0160
MPLE0170

2.4 显示上下文的配置项

⏹如下图所示的文本,我们想要匹配红框中的内容。

在这里插入图片描述

2.4.1 -C配置项

  • --context配置项的缩写,在匹配行的上方和下方显示额外的文本行。
  • -C 3:显示匹配行的上下方的3行
grep -a -C 3 -e "uuid=\S*" ./content.txt

在这里插入图片描述

2.4.2 -数字配置项

  • 本质上是 -C 配置项的简写
# 以下这3种的写法的作用相同
grep -a -3 "南山 裕三" ./content2.txt
grep -a3 "南山 裕三" ./content2.txt
grep -a -C 3 "南山 裕三" ./content2.txt

在这里插入图片描述

2.4.3 -A配置项

  • --after-context配置项的缩写,在匹配行的下方显示额外的文本行。
  • -A 3:显示匹配行的下方的3行。
grep -a -A 3 -e "uuid=\S*" ./content.txt

在这里插入图片描述

2.4.4 -B配置项

  • --before-context配置项的缩写,在匹配行的下方显示额外的文本行。
  • -B 3:显示匹配行的下方的3行。
grep -a -B 3 -e "uuid=\S*" ./content.txt

在这里插入图片描述


三. zgrep

⏹主要用来查询压缩包中的信息

# 统计 Desktop.zip 压缩包中 fengyehong 这个单词出现的次数
zgrep -a -e fengyehong /home/fengyehong/jmw_work_space/Desktop.zip | wc -l

四. 配合正则表达式查询

4.1 {} 或查询

{}表示或,{0[6-9],10}表示从06月到10月。

grep -a -e Exception /home/fengyehong/jmw_work_space/TEST-2023-{0[6-9],10}-11.txt
fengyehong@ubuntu:~$ grep -a -e Exception /home/fengyehong/jmw_work_space/TEST-2023-{0[6-9],10}-11.txt
/home/fengyehong/jmw_work_space/TEST-2023-07-11.txt:MPLE0130 Exception 123 ExecTime=
/home/fengyehong/jmw_work_space/TEST-2023-07-11.txt:MPLE0160 Exception 123 ExecTime=
/home/fengyehong/jmw_work_space/TEST-2023-09-11.txt:CCCE0130 Exception 123
/home/fengyehong/jmw_work_space/TEST-2023-09-11.txt:MPLE0160 Exception 123
/home/fengyehong/jmw_work_space/TEST-2023-10-11.txt:MPLE0130 Exception 123 ExecTime=
/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*

⏹查询带test前缀和不带test前缀的20240331_app.log文件

  • 从test20240331_app.log 或 20240331_app.log 中进行查询
grep -a content ./{test,}20240331_app.log

4.2 ^\S\S*

⏹路径和查询关键词中均包含正则表达式

  • ^\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
fengyehong@ubuntu:~$ grep -a -e "^\S" -e "ExecTime=\S*" /home/fengyehong/jmw_work_space/TEST-2023-{0[6-9],10}-11.txt
/home/fengyehong/jmw_work_space/TEST-2023-07-11.txt:MPLE0130 Exception 123 ExecTime=
/home/fengyehong/jmw_work_space/TEST-2023-07-11.txt:MPLE0190 ExecTime=123
/home/fengyehong/jmw_work_space/TEST-2023-07-11.txt:MPLE0150 TST 1234 ExecTime=454
/home/fengyehong/jmw_work_space/TEST-2023-07-11.txt:MPLE0160 Exception 123 ExecTime=
/home/fengyehong/jmw_work_space/TEST-2023-07-11.txt:MPLE0170 TST 1234 ExecTime=999
/home/fengyehong/jmw_work_space/TEST-2023-09-11.txt:CCCE0130 Exception 123
/home/fengyehong/jmw_work_space/TEST-2023-09-11.txt:MPLE0150 TST 1234
/home/fengyehong/jmw_work_space/TEST-2023-09-11.txt:MPLE0160 Exception 123
/home/fengyehong/jmw_work_space/TEST-2023-09-11.txt:CCCE0170 TST 1234
/home/fengyehong/jmw_work_space/TEST-2023-10-11.txt:MPLE0130 Exception 123 ExecTime=
/home/fengyehong/jmw_work_space/TEST-2023-10-11.txt:MPLE0190 ExecTime=123
/home/fengyehong/jmw_work_space/TEST-2023-10-11.txt:MPLE0150 TST 1234 ExecTime=454
/home/fengyehong/jmw_work_space/TEST-2023-10-11.txt:MPLE0160 Exception 123 ExecTime=
/home/fengyehong/jmw_work_space/TEST-2023-10-11.txt:MPLE0170 TST 1234 ExecTime=999

4.3 [^ ]*\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

4.4 ()|

📚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

4.5 ^\s

result.log

65457-1 2024/07/20 18:50:21 AAA BBB
65457-2 2024/07/20 18:52:21 CCC DDD
65457-11 2024/07/20 18:53:21 EEE FFF
65457-22 2024/07/20 18:53:21 GGG HHH

grep -a "^65457-1" result.log

  • 65457-11开头的数据也被检索出来
fengyehong@ubuntu:~/jmw_work_space/20270720$ grep -a "^65457-1" result.log
65457-1 2024/07/20 18:50:21 AAA BBB
65457-11 2024/07/20 18:53:21 EEE FFF

grep -a "^65457-1\s2024" result.log

  • \s代表空白字符(空格,TAB制表符,其他空白)
fengyehong@ubuntu:~/jmw_work_space/20270720$ grep -a "^65457-1\s2024" result.log
65457-1 2024/07/20 18:50:21 AAA BBB

五. 案例

5.1 查询两个文件第一列的数据并去重

📚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命令之后就是

  • 通过cat命令同时打开两个文本文件
  • -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

5.2 抽取日志中指定的字段

📚有如下日志file4.log

  • 日志分内容分为2类,SEQIN 和 SEQOUT
  • SEQIN 类中存在若干字段内容,isuuePayId,uuid,jmw_state,method等等
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

5.3 服务器指定时间点异常查询

  • 需求:由于上午发生网络波动,导致部分业务发生了异常
    • 现在需要查询出网络波动时间点和异常的件数
    • 哪个服务器发生的异常
    • 具体发生了什么的异常

⏹查询发生的件数版本1

  • 以ERROR 或 Exception 为关键词进行检索
  • c1A03a,c1Ae*a,c1Be*a都是服务器的名称
  • 同一个请求可能会发生多个异常,因此这种查询方式得到的件数可能会存在重复
grep -ae "\[ERROR\]" -ae "Exception" /logback/{c1A03a,c1Ae*a,c1Be*a}/tomcat/current/logs/
spl/.20240331*mpl*app.log | wc -l

⏹查询发生的件数版本2

grep -ae "\[ERROR\]" -ae "Exception" /logback/{c1A03a,c1Ae*a,c1Be*a}/tomcat/current/logs/
spl/.20240331*mpl*app.log | grep -a 09:0[5-9]:[0-5][0-9] | egrep -o "^\S*" | sort 
| uniq -c | wc -l

⏹查询发生异常的服务器名称

  • 根据时间点缩小范围
  • 查询开头不为空的部分(含有日志路径和线程号部分)
  • 排序之后去重
  • 之所以会用awk '{print $1}'是因为排序之后去重之后,获取到的内容之前会加上一个数字,用来表示当前行的重复数量
  • 若仅排序去重,不使用awk '{print $1}'处理,数据的大致样式如下
       1 /logback/c1Ae01a/tomcat/current/logs/spl/.2024033109mpl_test1_app.log:03210429-47789
       5 /logback/c1Ae12a/tomcat/current/logs/spl/.2024033109mpl_test2_app.log:03210154-24389
       3 /logback/c1Be07a/tomcat/current/logs/spl/.2024033109mpl_bis01_app.log:06334561-95267
    
  • 将查询结果放入result.log文件中
grep -ae "\[ERROR\]" -ae "Exception" /logback/{c1A03a,c1Ae*a,c1Be*a}/tomcat/current/logs/
spl/.20240331*mpl*app.log | grep -a 09:0[5-9]:[0-5][0-9] | egrep -o "^\S*" | sort 
| uniq -c | awk '{print $1}' > result.log

⏹result.log的内容如下

  • :的部分是日志所在的路径,从路径中可以找出服务器的名称
  • :的部分是请求的线程号,通过该线程号可以详细的抽出日志
/logback/c1Ae01a/tomcat/current/logs/spl/.2024033109mpl_test1_app.log:03210429-47789
/logback/c1Ae12a/tomcat/current/logs/spl/.2024033109mpl_test2_app.log:03210154-24389
/logback/c1Be07a/tomcat/current/logs/spl/.2024033109mpl_bis01_app.log:06334561-95267

5.4 从csv文件中抽取指定的数据

sftp_data.csv

AAA,222,333,20240604,20
AAA,222,333,20240604,10
BBB,444PPP,555SS,20240605,20
BBB,444,555SS,20240610,20
CCC,555,666SS,20240605,10
CCC,666,666SS,20240603,20

🤔需要从上面的csv文件中抽取

  • 第四列的从20240601~20240605的数据
  • 第五列为20的数据

grep "^.*,.*,.*,2024060[1-5],20" sftp_data.csv

  • 前3列为任意值
  • 后两列指定正则表达式和具体的值
fengyehong@ubuntu:~/jmw_work_space/20270720$ grep "^.*,.*,.*,2024060[1-5],20" sftp_data.csv
AAA,222,333,20240604,20
BBB,444PPP,555SS,20240605,20
CCC,666,666SS,20240603,20
  • 7
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值