Linux命令手册-文本内容搜索 grep命令(详解)

一、前言

grep作为linux系统下最重要、最常用的命令之一,本篇将对grep多种非常实用的十种使用方式进行讲解。

命令格式:

grep [选项] [模式] [文件名]

文本数据:

先随便运行一个项目,生成日志文件,在生成的日志文件中根据需求进行搜索。

打开其中一个日志文件,看下文件内容,以start.out文件为例,内容如下:

 

 这里都是项目运行的日志信息, 然后开始使用grep进行内容搜索

二、选项参数

以最为常用的选项参数为例,进行逐个讲解选项参数的实际应用。

三、命令示范 

场景一:在当前目录指定文件(start.out)中搜索关键词 starting

[root@localhost logs]# grep "starting" start.out

2022-06-25 20:12:03,968 INFO Nacos is starting...

2022-06-25 20:12:04,973 INFO Nacos is starting...

2022-06-25 20:12:05,988 INFO Nacos is starting...

2022-06-25 20:12:06,998 INFO Nacos is starting...

[root@localhost logs]#

在搜索结果中显示了关键词的所在行信息内容,但是如果像第一张图那样,有多个日志文件,并不知道我们的关键词在哪些文件中,该如何搜索。

场景二:在当前目录多个文件中搜索关键词 starting

[root@localhost logs]# grep "starting" *

nacos.log.2022-04-07.0:2022-04-07 11:01:33,426 INFO Nacos is starting...

nacos.log.2022-04-07.0:2022-04-07 11:01:34,449 INFO Nacos is starting...

nacos.log.2022-04-07.0:2022-04-07 11:01:35,457 INFO Nacos is starting...

nacos.log.2022-04-07.0:2022-04-07 11:01:36,458 INFO Nacos is starting...

nacos.log.2022-04-07.0:2022-04-07 11:54:29,844 INFO Nacos is starting...

nacos.log.2022-04-07.0:2022-04-07 11:54:30,858 INFO Nacos is starting...

nacos.log.2022-04-07.0:2022-04-07 11:54:31,860 INFO Nacos is starting...

nacos.log.2022-04-07.0:2022-04-07 11:54:32,864 INFO Nacos is starting...

nacos.log.2022-04-07.0:2022-04-07 22:10:26,564 INFO Nacos is starting...

nacos.log.2022-04-07.0:2022-04-07 22:10:27,566 INFO Nacos is starting...

nacos.log.2022-04-07.0:2022-04-07 22:10:28,571 INFO Nacos is starting...

nacos.log.2022-04-07.0:2022-04-07 22:10:29,576 INFO Nacos is starting...

... ...

这里虽然搜到了结果,如果在几十个文件中都有 starting 关键词,并且每个文件中starting出现次数较多时,搜索结果太多将会导致查看困难。

通过wc命令统计,找到了141个匹配项

[root@localhost logs]# grep "starting" *| wc -l

141

[root@localhost logs]#

下一步我们需要筛选重点文件

场景三: 统计当前目录下所有文件中哪些文件包含关键词(error)

[root@localhost logs]# grep -l "error" *

alipay-jraft.log.2022-04-08.0

alipay-jraft.log.2022-06-28.0

config-fatal.log

config-fatal.log.2022-04-08.0

config-server.log.2022-04-08.0

core-auth.log

nacos-cluster.log

nacos-cluster.log.2022-04-07.0

nacos-cluster.log.2022-04-08.0

nacos-cluster.log.2022-06-25.0

nacos-cluster.log.2022-06-26.0

nacos-cluster.log.2022-06-27.0

nacos-cluster.log.2022-06-28.0

nacos.log.2022-04-07.0

nacos.log.2022-04-08.0

nacos.log.2022-06-25.0

naming-raft.log.2022-04-07.0

protocol-raft.log.2022-04-07.0

protocol-raft.log.2022-04-08.0

protocol-raft.log.2022-06-26.0

protocol-raft.log.2022-06-27.0

protocol-raft.log.2022-06-28.0

通过 -l 选项可以只保留文件名,也就是在这些文件中存在starting关键词

场景四:统计当前目录下对应文件中关键词error出现的行数

以nacos-cluster.log前缀为例,通过 -c 参数搜索 这些文件中出现error的行数

[root@localhost logs]# grep -c "error" nacos-cluster.log*

nacos-cluster.log:3

nacos-cluster.log.2022-04-07.0:593

nacos-cluster.log.2022-04-08.0:11070

nacos-cluster.log.2022-06-25.0:16

nacos-cluster.log.2022-06-26.0:38

nacos-cluster.log.2022-06-27.0:26

nacos-cluster.log.2022-06-28.0:83

注意,这里是行数,并不是统计的个数,创建一个test.txt文件,内容如下:

[root@localhost logs]# cat test.txt

AABBBBBBBBBBBAA

AA

[root@localhost logs]#

统计AA和BB的行数:

[root@localhost logs]# grep "AA" test.txt -c

2

[root@localhost logs]# grep "BB" test.txt -c

1

[root@localhost logs]#

结果显示:AA有两行,BB有一行,与test.txt内容相符。

场景五:搜索关键词时,尤其是关键词为英文时,可能存在的大小写导致搜索遗漏

如图,在test.txt文件搜索 “aa” 时,没有匹配,搜索 “AA”时有两个匹配项

[root@localhost logs]# grep "aa" test.txt

[root@localhost logs]# grep -i "aa" test.txt

AABBBBBBBBBBBAA

AA

[root@localhost logs]#

加入 -i 选项,可忽略大小写,通过加上 -c 进行统计个数也可以验证:

[root@localhost logs]# grep -c "aa" test.txt

0

[root@localhost logs]# grep -ic "aa" test.txt

2

[root@localhost logs]#

场景六:如果想显示搜索结果所在的行号,可通过-n参数

[root@localhost logs]# grep -n "AA" test.txt

1:AABBBBBBBBBBBAA

2:AA

[root@localhost logs]#

行号在一些较大的文件中搜索时,可以通过行号快速定位。

场景七:有时候我们需要统计数匹配内容的个数,可以使用 -o 参数

[root@localhost logs]# grep -o "B" test.txt

B

B

B

B

B

B

B

B

B

B

B

[root@localhost logs]# grep -o "B" test.txt |wc -l

11

[root@localhost logs]#

场景八:配合正则表达式,可以关键词出现指定次数进行设定

匹配规则为:{n,m}
{n,m}:匹配前一个字符至少n次, 最多m次
{n,}: 匹配前一个字符至少n次, 没有上限
{,m}: 匹配前一个字符最多m次,可以没有
重复前一个字符各种次数, 可以通过-o参数显示

[root@localhost logs]# grep -E "B{1,20}" test.txt

AABBBBBBBBBBBAA

[root@localhost logs]# grep -E "B{10,20}" test.txt

AABBBBBBBBBBBAA

[root@localhost logs]# grep -E "B{15,20}" test.txt

[root@localhost logs]#

场景九:查看一段时间的日志

在高并发日志打印频繁时,可以通过限制时间段的方式进行搜索。

如下搜索打印时间在2022-04-08 20:26:20到2022-04-08 20:26:29之间的信息

[root@localhost logs]#

[root@localhost logs]# grep "2022-04-08 20:26:2[0-9]"  nacos-cluster.log.2022-04-08.0

2022-04-08 20:26:22,907 ERROR failed to report new info to target node : 10.211.55.11:8848, error : caused: 没有到主机的路由;

2022-04-08 20:26:26,029 ERROR failed to report new info to target node : 10.211.55.11:8848, error : caused: 没有到主机的路由;

2022-04-08 20:26:26,030 ERROR failed to report new info to target node : 10.211.55.10:8848, error : caused: 没有到主机的路由;

2022-04-08 20:26:29,149 ERROR failed to report new info to target node : 10.211.55.10:8848, error : caused: 没有到主机的路由;

2022-04-08 20:26:29,149 ERROR failed to report new info to target node : 10.211.55.11:8848, error : caused: 没有到主机的路由;

[root@localhost logs]#

场景十:同一行中多关键词查询

1.同一行中多关键字,OR

[root@localhost logs]# grep "AA\|BB" test.txt

AABBBBBBBBBBBAA

AA

[root@localhost logs]# grep -E "AA|BB" test.txt

AABBBBBBBBBBBAA

AA

[root@localhost logs]#

2.同一行中多个关键字,AND

[root@localhost logs]# grep -E 'AA' test.txt | grep -E 'BB'

AABBBBBBBBBBBAA

[root@localhost logs]#

3.同一行中没有多个关键字,NOT

[root@localhost logs]# grep -v -E "BB" test.txt

AA

[root@localhost logs]#

  • 2
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

月夜烛峰

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值