grep命令应用

与之容易混淆的是egrep命令和fgrep命令。如果把grep命令当作是标准搜索命令,那么egrep则是扩展搜索命令,等价于“grep -E”命令,支持扩展的正则表达式。而fgrep则是快速搜索命令,等价于“grep -F”命令,不支持正则表达式,直接按照字符串内容进行匹配。

1.语法格式:

grep[参数] 文件


常用参数:

-i 忽略大小写

-c 只输出匹配行的数量

-l 只列出符合匹配的文件名,不列出具体的匹配行

-n 列出所有的匹配行,显示行号

-h 查询多文件时不显示文件名

-s 不显示不存在、没有匹配文本的错误信息

-v 显示不包含匹配文本的所有行

-w 匹配整词

-x 匹配整行

-r 递归搜索

-q 禁止输出任何结果,已退出状态表示搜索是否成功

-b 打印匹配行距文件头部的偏移量,以字节为单位

-o 与-b结合使用,打印匹配的词据文件头部的偏移量,以字节为单位

-F 匹配固定字符串的内容

-E 支持扩展的正则表达式

2.示例

1.搜索某个文件中,包含某个关键词的内容

[root@linuxcool ~]# grep root /etc/passwd

root:x:0:0:root:/root:/bin/bash

operator:x:11:0:operator:/root:/sbin/nologin

2.搜索某个文件中,以某个关键词开头的内容:

[root@linuxcool ~]# grep ^root /etc/passwd

root:x:0:0:root:/root:/bin/bash

3.搜索多个文件中,包含某个关键词的内容:

[root@linuxcool ~]# grep linuxprobe /etc/passwd /etc/shadow

/etc/passwd:linuxprobe:x:1000:1000:linuxprobe:/home/linuxprobe:/bin/bash

/etc/shadow:linuxprobe:$6$9Av/41hCM17T2PrT$hoggWJ3J/j6IqEOSp62elhdOYPLhQ1qDho7hANcm5fQkPCQdib8KCWGdvxbRvDmqyOarKpWGxd8NAmp3j2Ln00::0:99999:7:::

4.搜索多个文件中,包含某个关键词的内容,不显示文件名称:

[root@linuxcool ~]# grep -h linuxprobe /etc/passwd /etc/shadow

linuxprobe:x:1000:1000:linuxprobe:/home/linuxprobe:/bin/bash

linuxprobe:$6$9Av/41hCM17T2PrT$hoggWJ3J/j6IqEOSp62elhdOYPLhQ1qDho7hANcm5fQkPCQdib8KCWGdvxbRvDmqyOarKpWGxd8NAmp3j2Ln00::0:99999:7:::

5.输出在某个文件中,包含某个关键词行的数量:

[root@linuxcool ~]# grep -c root /etc/passwd /etc/shadow

/etc/passwd:2

/etc/shadow:1

6.搜索某个文件中,包含某个关键词位置的行号及内容:

[root@linuxcool ~]# grep -n network anaconda-ks.cfg

17:network --bootproto=static --device=ens160 --ip=192.168.10.10 --netmask=255.255.255.0 --onboot=off --ipv6=auto --activate

18:network --hostname=linuxcool.com

7.搜索某个文件中,不包含某个关键词的内容:

[root@linuxcool ~]# grep -v nologin /etc/passwd

root:x:0:0:root:/root:/bin/bash

sync:x:5:0:sync:/sbin:/bin/sync

shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown

halt:x:7:0:halt:/sbin:/sbin/halt

linuxprobe:x:1000:1000:linuxprobe:/home/linuxprobe:/bin/bash

8.搜索当前工作目录中,包含某个关键词内容的文件,未找到则提示:

[root@linuxcool ~]# grep -l root *

anaconda-ks.cfg

grep: Desktop: Is a directory

grep: Documents: Is a directory

grep: Downloads: Is a directory

initial-setup-ks.cfg

grep: Music: Is a directory

grep: Pictures: Is a directory

grep: Public: Is a directory

grep: Templates: Is a directory

grep: Videos: Is a directory

9.搜索当前工作目录中,包含某个关键词内容的文件,未找到不提示:

[root@linuxcool ~]# grep -sl root *

anaconda-ks.cfg

initial-setup-ks.cfg

10.递归搜索,不仅搜索指定目录,还搜索其内子目录内是否有关键词文件:

[root@linuxcool ~]# grep -srl root /etc

/etc/fstab

/etc/X11/xinit/Xclients

/etc/X11/xinit/xinitrc

/etc/libreport/events.d/collect_dnf.conf

/etc/libreport/events.d/bugzilla_anaconda_event.conf

/etc/libreport/forbidden_words.conf

………………省略部分输出信息………………

11.搜索某个文件中,精准匹配到某个关键词的内容(搜索词应与整行内容完全一样才会显示,有别于一般搜索):

[root@linuxcool ~]# grep -x cd anaconda-ks.cfg

[root@linuxcool ~]# grep -x cdrom anaconda-ks.cfg

cdrom

12.判断某个文件中,是否包含某个关键词,通过返回状态值输出结果(0为包含,1为不包含),方便在Shell脚本中判断和调用:

[root@linuxcool ~]# grep -q linuxprobe anaconda-ks.cfg

[root@linuxcool ~]# echo $?

0

[root@linuxcool ~]# grep -q linuxcool anaconda-ks.cfg

[root@linuxcool ~]# echo $?

1

13.搜索某个文件中,空行的数量:

[root@linuxcool ~]# grep -c ^$ anaconda-ks.cfg