grep 用法

2021-09-08  本文已影响0人  LY_7z

https://www.zsythink.net/archives/1733

我们可以使用grep命令在文本中查找指定的字符串,可以把grep理解成字符查找工具。

[root@centos7 tmp]# grep "zabbix" /etc/passwd
zabbix:x:988:982:Zabbix Monitoring System:/var/lib/zabbix:/sbin/nologin

默认情况下,grep是区分大小写的,使用 -i 选项搜索时忽略大小写。

[root@centos7 tmp]# grep -i "zabbix" /etc/passwd
zabbix:x:988:982:Zabbix Monitoring System:/var/lib/zabbix:/sbin/nologin
ZAbbix:x:1058:1058::/home/ZAbbix:/bin/bash

如果我们想确定zabbix用户在passwd文件的第一行,使用-n选项,显示文本所在行号。

[root@centos7 tmp]# grep -i -n "zabbix" /etc/passwd
46:zabbix:x:988:982:Zabbix Monitoring System:/var/lib/zabbix:/sbin/nologin
50:ZAbbix:x:1058:1058::/home/ZAbbix:/bin/bash

如果想知道文件中有多少行包含了指定字符串,使用-c选项可只统计符号条件的行,而不打印出来。

[root@centos7 tmp]# grep -i -c 'zabbix' /etc/passwd
2

如果我们只想看到被匹配的关键字,而不是把关键字所在的整行都打印出来,使用-o选项,但是需要注意,-o选项会把每个匹配到的关键字都单独显示在一行中进行输出。

[root@centos7 tmp]# grep -i -o -n 'zabbix' /etc/passwd
46:zabbix
46:Zabbix
46:zabbix
50:ZAbbix
50:ZAbbix

显示关键字附件的信息 -A after -B before -C content

[root@centos7 tmp]# grep -B2 'zhangsan' /etc/passwd
zabbix:x:988:982:Zabbix Monitoring System:/var/lib/zabbix:/sbin/nologin
grafana:x:987:981:grafana user:/usr/share/grafana:/sbin/nologin
zhangsan:x:800:1059:AdminUser:/tmp/zhangsan:/bin/sh

[root@centos7 tmp]# grep -A2 'zhangsan' /etc/passwd
zhangsan:x:800:1059:AdminUser:/tmp/zhangsan:/bin/sh
oldboy:x:1057:1057::/home/oldboy:/bin/bash
ZAbbix:x:1058:1058::/home/ZAbbix:/bin/bash

[root@centos7 tmp]# grep -C1 'zhangsan' /etc/passwd
grafana:x:987:981:grafana user:/usr/share/grafana:/sbin/nologin
zhangsan:x:800:1059:AdminUser:/tmp/zhangsan:/bin/sh
oldboy:x:1057:1057::/home/oldboy:/bin/bash

如果我们需求精确匹配,就是搜索的关键字作为一个独立的单词存在,而不是包含在某个字符串中,使用-w选项,这时候nologin就没有被匹配到。

[root@centos7 tmp]# grep -w 'login' /etc/passwd
zhangsan:x:1058:1058::/home/zhangsan:/bin/login

如果想取反,就是查找不包含指定字符串的行,使用-v选项。

[root@centos7 tmp]# grep -v -i -n 'nologin' /etc/passwd
1:root:x:0:0:root:/root:/bin/bash
6:sync:x:5:0:sync:/sbin:/bin/sync
7:shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
8:halt:x:7:0:halt:/sbin:/sbin/halt
48:oldboy:x:1057:1057::/home/oldboy:/bin/bash
49:zhangsan:x:1058:1058::/home/zhangsan:/bin/login

如果想同时匹配多个目标,使用-e选项,他们之间是“或”的关系。

[root@centos7 tmp]# grep -e 'zhang'  -e 'zabbix' /etc/passwd
zabbix:x:988:982:Zabbix Monitoring System:/var/lib/zabbix:/sbin/nologin
zhangsan:x:1058:1058::/home/zhangsan:/bin/login

如果只是想利用grep判断文本中是否包含某个字符串,你只关心有没有匹配到,而不想输出,可以使用-q选项,开启静默模式。

[root@centos7 tmp]# grep -q 'zhang'  /etc/passwd
[root@centos7 tmp]# echo $?
0

[root@centos7 tmp]# grep -q -w 'zhang'  /etc/passwd
[root@centos7 tmp]# echo $?
1

掌握以上用法,基础的就够了,等学习了“正则表达式”,再回来结合一起发挥威力。

grep用法总结:

-i:在搜索的时候忽略大小写
-n:显示结果所在行号
-c:统计匹配到的行数,注意,是匹配到的总行数,不是匹配到的次数
-o:只显示符合条件的字符串,但是不整行显示,每个符合条件的字符串单独显示一行
-v:输出不带关键字的行(反向查询,反向匹配)
-w:匹配整个单词,如果是字符串中包含这个单词,则不作匹配
-Ax:在输出的时候包含结果所在行之后的指定行数,这里指之后的x行,A:after
-Bx:在输出的时候包含结果所在行之前的指定行数,这里指之前的x行,B:before
-Cx:在输出的时候包含结果所在行之前和之后的指定行数,这里指之前和之后的x行,C:context
-e:实现多个选项的匹配,逻辑or关系
-q:静默模式,不输出任何信息,当我们只关心有没有匹配到,却不关心匹配到什么内容时,我们可以使用此命令,然后,使用”echo $?”查看是否匹配到,0表示匹配到,1表示没有匹配到。
-P:表示使用兼容perl的正则引擎。
-E:使用扩展正则表达式,而不是基本正则表达式,在使用”-E”选项时,相当于使用egrep。

grep结合正则表达式

字符类 [ ] 括号内的仅匹配其中一个字符
如果我想要搜寻 test 或 taste 这两个单字时,可以发现到,其实她们有共通的 't?st'

[root@centos7 tmp]# grep -n 't[ae]st' test
2:i can't finish the test
3:oh! the soup taste good!!!

字符类的反向选择 [^]
连续字符 [a-zA-Z0-9]

[root@centos7 tmp]# cat test
abc#123#def#456
i can't finish the test
oh! the soup taste good!!!
football game is not use feet only
gooooood bye!
google is the best tools for search keyword

如果想要搜索到有 oo 的行,但不想要 oo 前面有 g
[root@centos7 tmp]# grep -n  '[^g]oo' test
4:football game is not use feet only
5:gooooood bye!
6:google is the best tools for search keyword
我们发现把google也搜索出来了,这好像和我们的预期不符合,为什么呢?因为后面有符号的tools,所以这一行也是符号的。

如果不想oo之前出现小写字母
[root@centos7 tmp]# grep -n '[^a-z]oo' test
4:Football game is not use feet only

匹配行首 ^
结合[]时要注意^的位置
匹配行尾 $

[root@centos7 tmp]# grep -n  '^i' test
2:i can't finish the test

[root@centos7 tmp]# grep -n  '^[A-Z]' test
4:Football game is not use feet only

匹配不是以字母开头的行,注意^的位置,在 [] 内代表『反向选择』,在 [] 之外则代表定位在行首的意义
[root@centos7 tmp]# grep '^[^a-zA-Z]' test
222abc#123#def#456
555 gooooood bye!


[root@centos7 tmp]# grep -n  'd$' test
3:oh! the soup taste good
6:google is the best tools for search keyword

如果想找出以 . 结尾的行,因为 . 具有其他含义,必须转义\.
[root@centos7 tmp]# grep '\.$' test
oh! the soup taste good.
google is the best tools for search keyword.

找出空白行使用  grep  '^$'

我们来看 . 和 * 在正则中的含义
. 表示任意一个字符
星号*表示重复他前面的字符0次到无穷多次

[root@centos7 tmp]# grep 'g..d' test
oh! the soup taste good.

好好折磨下面这个示例,*号前面的o可以出现0次,也可以出现多次
[root@centos7 tmp]# grep 'ooo*' test
oh! the soup taste good.
Football game is not use feet only
555 gooooood bye!
google is the best tools for search keyword.

[root@centos7 tmp]# grep 'goo*d' test
oh! the soup taste good.
555 gooooood bye!

[root@centos7 tmp]# grep 'g.*d' test
oh! the soup taste good.
555 gooooood bye!
google is the best tools for search keyword.

慢慢品味区别
[root@centos7 tmp]# grep '[0-9]*' test
222abc#123#def#456
i can't finish the test
oh! the soup taste good.
Football game is not use feet only
555 gooooood bye!
google is the best tools for search keyword.

[root@centos7 tmp]# grep '[0-9][0-9]*' test
222abc#123#def#456
555 gooooood bye!

限定范围的字符{} ,但因为 { 与 } 的符号在 shell 是有特殊意义的,因此, 我们必须要使用字符 \ 来让他失去特殊意义才行。

假设我要找到两个 o 的字串
[root@centos7 tmp]# grep 'o\{2\}'  test
oh! the soup taste good.
Football game is not use feet only
555 gooooood bye!
google is the best tools for search keyword.

假设我们要找出 g 后面接 2 到 5 个 o ,然后再接一个 d的字符串
[root@centos7 tmp]# grep 'go\{2,5\}d'  test
oh! the soup taste good.
goooood bye!
goodle is the best tools for search keyword.

[root@centos7 tmp]# grep 'go\{2,\}d'  test

使用扩展grep -E

[root@centos7 tmp]# free -h | egrep 'Mem|Swap'
Mem:           7.8G        3.5G        1.4G        231M        2.9G        3.7G
Swap:          6.0G        264K        6.0G

如果使用grep将没有东西输出,如果在扩展元字符前面加\,grep会自动启用扩展选项-E。
[root@centos7 tmp]# free -h | grep 'Mem|Swap'
[root@centos7 tmp]# free -h | grep 'Mem\|Swap'
Mem:           7.8G        3.5G        1.4G        231M        2.9G        3.7G
Swap:          6.0G        264K        6.0G

搜索包含一个或多个字母o的行
[root@centos7 tmp]# egrep 'o+' test
oh! the soup taste good.
Football game is not use feet only
goooood bye!
goodle is the best tools for search keyword.

[root@centos7 tmp]# grep -E 'o+'  test
oh! the soup taste good.
Football game is not use feet only
goooood bye!
goodle is the best tools for search keyword.

[root@centos7 tmp]# grep  'o\+'  test
oh! the soup taste good.
Football game is not use feet only
goooood bye!
goodle is the best tools for search keyword.

搜索所有包含0个或1个数字的行。
[root@centos7 tmp]# egrep '2\.?[0-9]' test
222abc#123#def#456

[root@centos7 tmp]# grep -E  '(go)+' test
oh! the soup taste good.
goooood bye!
goodle is the best tools for search keyword.

fgrep 查询速度比grep命令快,但是不够灵活:它只能找固定的文本,而不是规则表达式。
在文本中找出包含#的行

[root@centos7 tmp]# grep -F '#' test
222abc#123#def#456
上一篇下一篇

猜你喜欢

热点阅读