grep命令

grep 入门招式
grep 是能力最全面的,下面我们就先来学习 grep,围绕 /etc/passwd 文件来讲解 grep 的作用。
[root@roclinux ~]# cat /etc/passwd
root❌0:0:root:/root:/bin/bash
bin❌1:1:bin:/bin:/sbin/nologin
daemon❌2:2:daemon:/sbin:/sbin/nologin
adm❌3:4:adm:/var/adm:/sbin/nologin
lp❌4:7:lp:/var/spool/lpd:/sbin/nologin
(中间省略数十行)
apache❌48:48:Apache:/var/www:/sbin/nologin
test❌502:502::/home/test:/bin/bash
leo❌503:503::/home/leo:/bin/bash
roc❌504:504::/home/roc:/bin/bash
尝试搜索包含leo字符串的行:
[root@roclinux ~]# grep --color “leo” /etc/passwd
leo❌503:503::/home/leo:/bin/bash

注意上面的–color选项,它的作用是高亮我们查找的字符串,这里,leo 字符串变成了红色字体。
grep 的反查技能
下面来搜索不包含 leo 字符串的行。

使用-v选项

[root@roclinux ~]# grep -v “leo” /etc/passwd
root❌0:0:root:/root:/bin/bash
bin❌1:1:bin:/bin:/sbin/nologin
daemon❌2:2:daemon:/sbin:/sbin/nologin
adm❌3:4:adm:/var/adm:/sbin/nologin
lp❌4:7:lp:/var/spool/lpd:/sbin/nologin
(中间省略数十行)
apache❌48:48:Apache:/var/www:/sbin/nologin
test❌502:502::/home/test:/bin/bash
roc❌504:504::/home/roc:/bin/bash

我们使用-v选项实现了反查效果,可以看到,含有 leo 的行都没有展示出来。
grep 展示行号和统计行数
有时,我们希望 grep 不仅能搜索到字符串,还能展示出它们位于文件的第几行,这时我们可以使用-n选项来实现这个效果。

来看看包含 leo 的行位于文件的第几行。

使用选项 n

[root@roclinux ~]# grep -n leo /etc/passwd
29♌️x:503:503::/home/leo:/bin/bash

大家注意观察输出内容的开头部分,leo 前的内容表示的就是行位置信息,原来是在第 29 行呀!

另外一些时候,我们希望 grep 不要输出搜索到的行的内容,而是简单地告诉我们到底搜索到了多少行就好了,试试-c选项吧。

使用-c选项

[root@roclinux ~]# grep -c leo /etc/passwd
1
输出很简单,这表示含有 leo 的行只有 1 行。
grep 能环顾四周
我想搜索包含 leo 的行,但 grep 在输出时,最好能把 leo 所在行的上面或下面相邻的行也都展示出来。

我们来看看 grep 是如何做到的。
[root@roclinux ~]# grep -A 1 leo passwd
leo❌503:503::/home/leo:/bin/bash
roc❌504:504::/home/roc:/bin/bash

上面示例中的-A选项,是 After 的缩写,表示除了展示匹配行之外,还要展示出匹配行下面的若干行。而示例中的 -A 1 则表示还展示匹配行下面一行的内容。
[root@roclinux ~]# grep -B 1 leo passwd
test❌502:502::/home/test:/bin/bash
leo❌503:503::/home/leo:/bin/bash

上面示例中的-B选项,是 Before 的缩写,表示除了展示匹配行之外,还要展示出匹配行上面的若干行。而示例中的 -B 1 则表示还展示匹配行上面一行的内容。
[root@roclinux ~]# grep -C 1 leo passwd
test❌502:502::/home/test:/bin/bash
leo❌503:503::/home/leo:/bin/bash
roc❌504:504::/home/roc:/bin/bash

上面示例中使用了-C选项,它是-A和-B选项的合体,表示除了展示匹配行之外,还要展示出匹配行上面和下面各若干行。而示例中的 -C 1 则表示还展示匹配行上面一行和下面一行的内容。
让 grep 不要区分大小写
在搜索过程中,我们有时候希望不要区分字母的大小写,这样做可以提高搜索命中的概率,而-i选项则可以帮我们这个忙。
[root@roclinux ~]# grep -i “LEO” passwd
leo❌503:503::/home/leo:/bin/bash

看到了吧,即便我们把要搜索的字符串指定为全部大写的 LEO,仍然可以顺利地搜索到全部小写的 leo。
grep 处理多文件
grep 命令可以一次搜索很多个文件,最常使用的一个场景就是:从大量的文件中找出含有特定字符的文件。下面我们来试验一下。

当前目录下有三个文件

[roc@roclinux ~]$ ll
total 12
-rw-rw-r-- 1 roc roc 58 Mar 15 17:47 1.txt
-rw-rw-r-- 1 roc roc 59 Mar 15 17:51 2.txt
-rw-rw-r-- 1 roc roc 58 Mar 15 17:52 3.txt

1.txt文件的内容如下

[roc@roclinux ~]$ cat 1.txt
this first file
this file contain some import infomation.

2.txt文件的内容如下

[roc@roclinux ~]$ cat 2.txt
this second file
this file contain some import infomation.

3.txt文件的内容如下

[roc@roclinux ~]$ cat 3.txt
this third file
this file contain some import infomation.

我们的搜索需求是,找出内容中含有first单词的文件都有哪些。我们希望得到的是一个文件列表。

使用-l选项

[roc@roclinux ~]$ grep -l “first” *.txt
1.txt

原来只有 1.txt 文件中包含有 first 单词。如果我想找出不含 first 单词的文件都有哪些,该如何操作呢?
[roc@roclinux ~]$ grep -L “first” *.txt
2.txt
3.txt
反向操作就要用反向选项,只需把 -l 变成 -L 即可。
grep 也正则
上面讲解的内容,更多的是以普通字符串作为搜索对象的,接下来的内容里,我们会更多地设置一些复杂的搜索需求,让大家体会到正则的好处与威力。

我们希望搜索/etc/passwd文件中开头是 leo 的行:

^表示行首

[root@roclinux ~]# grep ‘^leo’ /etc/passwd
leo❌503:503::/home/leo:/bin/bash

看到了吧,我们使用“^leo”表达了“开头是 leo 的行”,而 grep 也心领神会,帮我们找到了所求。

那如果我们希望搜索 /etc/passwd 文件中行尾是 bash 的行呢?

$ 表示行尾

[root@roclinux ~]# grep 'bash ′ / e t c / p a s s w d r o o t : x : 0 : 0 : r o o t : / r o o t : / b i n / b a s h c l o u d − u s e r : x : 500 : 500 : : / h o m e / c l o u d − u s e r : / b i n / b a s h t e s t : x : 502 : 502 : : / h o m e / t e s t : / b i n / b a s h l e o : x : 503 : 503 : : / h o m e / l e o : / b i n / b a s h r o c : x : 504 : 504 : : / h o m e / r o c : / b i n / b a s h 使用“ b a s h ' /etc/passwd root:x:0:0:root:/root:/bin/bash cloud-user:x:500:500::/home/cloud-user:/bin/bash test:x:502:502::/home/test:/bin/bash leo:x:503:503::/home/leo:/bin/bash roc:x:504:504::/home/roc:/bin/bash 使用“bash /etc/passwdroot:x:0:0:root:/root:/bin/bashclouduser:x:500:500::/home/clouduser:/bin/bashtest:x:502:502::/home/test:/bin/bashleo:x:503:503::/home/leo:/bin/bashroc:x:504:504::/home/roc:/bin/bash使用bash”即可,你会发现正则表达式其实并不难。
有关“词”的知识
我们希望搜索含有 bin 这个词的行,按照原来我们学到的知识,一定会这样来搜索:
[root@roclinux ~]# grep bin /etc/passwd
root❌0:0:root:/root:/bin/bash
bin❌1:1:bin:/bin:/sbin/nologin
daemon❌2:2:daemon:/sbin:/sbin/nologin
adm❌3:4:adm:/var/adm:/sbin/nologin
lp❌4:7:lp:/var/spool/lpd:/sbin/nologin
(此处省略数十行)
apache❌48:48:Apache:/var/www:/sbin/nologin
test❌502:502::/home/test:/bin/bash
leo❌503:503::/home/leo:/bin/bash
roc❌504:504::/home/roc:/bin/bash

但是,你会发现,这样的搜索结果有一个问题,那就是连 sbin 这样的词也会被搜索出来,而我们并不想看到这样的结果。这个问题,算是一个比较难的问题了,需要我们了解“词”的定义,以及知道正则表达式中如何表示一个“词”。

首先,正则表达式中的“词(word)”,一般是由字母、数字和下划线所组成的,且词与词之间通常使用空格、制表符或换行符分隔。举个例子,“I love you.”中的“love”就是一个“词”,但“My gloves are red.”中的“love”就不能称作一个“词”。

在正则表达式中,我们通常用尖括号表示一个“词”,比如能够匹配“I love you.”,但不能匹配“My gloves are red.”。

下面我们通过示例学习这个知识点:
#我们尝试匹配bin这个“词”
[root@roclinux ~]# grep ‘<bin>’ /etc/passwd --color
root❌0:0:root:/root:/bin/bash
bin❌1:1:bin:/bin:/sbin/nologin
sync❌5:0:sync:/sbin:/bin/sync
cloud-user❌500:500::/home/cloud-user:/bin/bash
test❌502:502::/home/test:/bin/bash
leo❌503:503::/home/leo:/bin/bash
roc❌504:504::/home/roc:/bin/bash

我们通过使用<和>来准确匹配到了 bin 这个词,而 sbin 却被挡在了门外。

其实,上面的方法还是有些复杂了,grep 中还有一个更加简单的方法来实现对“词”的搜索:

使用-w选项来搜索一个词

[root@roclinux ~]# grep -w ‘bin’ /etc/passwd
root❌0:0:root:/root:/bin/bash
bin❌1:1:bin:/bin:/sbin/nologin
sync❌5:0:sync:/sbin:/bin/sync
cloud-user❌500:500::/home/cloud-user:/bin/bash
test❌502:502::/home/test:/bin/bash
leo❌503:503::/home/leo:/bin/bash
roc❌504:504::/home/roc:/bin/bash
grep 的多条件查找
如果我想查找以 root 为行首的或以 bash 为行尾的行,那应该怎么查找呢?这时就要有请我们的二师兄 egrep 来帮忙了。
[root@roclinux ~]# egrep ‘^root|bash$’ passwd
root❌0:0:root:/root:/bin/bash
cloud-user❌500:500::/home/cloud-user:/bin/bash
test❌502:502::/home/test:/bin/bash
leo❌503:503::/home/leo:/bin/bash
roc❌504:504::/home/roc:/bin/bash

我们通过 egrep 命令实现了两个条件的搜索,其中“|”符号表示“或”,连接了两个搜索条件。

同样的搜索需求,grep 就没有办法满足(除非使用-E选项):
[root@roclinux ~]# grep ‘^root|bash$’ passwd

这就是 grep 和 egrep的区别了,grep 的正则表达式是基本正则表达式,而 egrep 的正则表达式是扩展正则表达式,这两种表达式是有区别的,下面我们就一起来看看它们的区别。
基本正则表达式和扩展正则表达式
世界上的正则表达式种类繁多且复杂,面对这样的状况,POSIX 将正则表达式进行了标准化,并把实现方法分为了两大类:
基本正则表达式(BRE)
扩展正则表达式(ERE)

两者的区别,更多的是元字符的区别。

在基本正则表达式(BRE)中,只承认“^”、“$”、“.”、“[”、“]”、“*”这些是元字符,所有其他的字符都被识别为普通字符。

而在扩展正则表达式(ERE)中,则在BRE的基础上增加了“(”、“)”、“{”、“}”、“?”和“+”、“|”等元字符。

最后要特别说明的一点,只有在用反斜杠进行转义的情况下,字符“(”、“)”、“{”和“}”才会在扩展正则表达式(ERE)中被当作元字符处理,而在基本正则表达式(ERE)中,任何元字符前面加上反斜杠反而会使其被当作普通字符来处理。这样的设计,有些奇葩,同学们一定要记清楚哦。
三师弟 fgrep 最朴素
grep 和 egrep 都支持正则表达式,但 fgrep 却完全不支持正则表达式。
#匹配到了好多行
[root@roclinux ~]# grep -c ‘^root’ /etc/passwd
6688

#一行都没有匹配到
[root@roclinux ~]# fgrep -c ‘^root’ /etc/passwd
0
这个示例证明了 fgrep 的确不支持正则表达式。那 fgrep 还有用武之地么?答案是肯定的。

当我们搜索时,假如搜索字符串中包含了不少特殊字符,而这些特殊字符恰好又是正则表达式预留的字符,比如说“^”、“KaTeX parse error: Expected 'EOF', got '#' at position 77: …通字符。我们来看下面的示例。 #̲我们的roc.txt文件中有几…
[roc@roclinux ~]$ cat roc.txt
^this third file
^$this file contain some import infomation.

grep会尝试去找开头为this的行, 但并未找到

[roc@roclinux ~]$ grep ‘^this’ roc.txt

fgrep会老老实实地去找^this字符串, 它找到了

[roc@roclinux ~]$ fgrep ‘^this’ roc.txt
^this third file
grep 入门招式
grep 是能力最全面的,下面我们就先来学习 grep,围绕 /etc/passwd 文件来讲解 grep 的作用。
[root@roclinux ~]# cat /etc/passwd
root❌0:0:root:/root:/bin/bash
bin❌1:1:bin:/bin:/sbin/nologin
daemon❌2:2:daemon:/sbin:/sbin/nologin
adm❌3:4:adm:/var/adm:/sbin/nologin
lp❌4:7:lp:/var/spool/lpd:/sbin/nologin
(中间省略数十行)
apache❌48:48:Apache:/var/www:/sbin/nologin
test❌502:502::/home/test:/bin/bash
leo❌503:503::/home/leo:/bin/bash
roc❌504:504::/home/roc:/bin/bash
尝试搜索包含leo字符串的行:
[root@roclinux ~]# grep --color “leo” /etc/passwd
leo❌503:503::/home/leo:/bin/bash

注意上面的–color选项,它的作用是高亮我们查找的字符串,这里,leo 字符串变成了红色字体。
grep 的反查技能
下面来搜索不包含 leo 字符串的行。

使用-v选项

[root@roclinux ~]# grep -v “leo” /etc/passwd
root❌0:0:root:/root:/bin/bash
bin❌1:1:bin:/bin:/sbin/nologin
daemon❌2:2:daemon:/sbin:/sbin/nologin
adm❌3:4:adm:/var/adm:/sbin/nologin
lp❌4:7:lp:/var/spool/lpd:/sbin/nologin
(中间省略数十行)
apache❌48:48:Apache:/var/www:/sbin/nologin
test❌502:502::/home/test:/bin/bash
roc❌504:504::/home/roc:/bin/bash

我们使用-v选项实现了反查效果,可以看到,含有 leo 的行都没有展示出来。
grep 展示行号和统计行数
有时,我们希望 grep 不仅能搜索到字符串,还能展示出它们位于文件的第几行,这时我们可以使用-n选项来实现这个效果。

来看看包含 leo 的行位于文件的第几行。

使用选项 n

[root@roclinux ~]# grep -n leo /etc/passwd
29♌️x:503:503::/home/leo:/bin/bash

大家注意观察输出内容的开头部分,leo 前的内容表示的就是行位置信息,原来是在第 29 行呀!

另外一些时候,我们希望 grep 不要输出搜索到的行的内容,而是简单地告诉我们到底搜索到了多少行就好了,试试-c选项吧。

使用-c选项

[root@roclinux ~]# grep -c leo /etc/passwd
1
输出很简单,这表示含有 leo 的行只有 1 行。
grep 能环顾四周
我想搜索包含 leo 的行,但 grep 在输出时,最好能把 leo 所在行的上面或下面相邻的行也都展示出来。

我们来看看 grep 是如何做到的。
[root@roclinux ~]# grep -A 1 leo passwd
leo❌503:503::/home/leo:/bin/bash
roc❌504:504::/home/roc:/bin/bash

上面示例中的-A选项,是 After 的缩写,表示除了展示匹配行之外,还要展示出匹配行下面的若干行。而示例中的 -A 1 则表示还展示匹配行下面一行的内容。
[root@roclinux ~]# grep -B 1 leo passwd
test❌502:502::/home/test:/bin/bash
leo❌503:503::/home/leo:/bin/bash

上面示例中的-B选项,是 Before 的缩写,表示除了展示匹配行之外,还要展示出匹配行上面的若干行。而示例中的 -B 1 则表示还展示匹配行上面一行的内容。
[root@roclinux ~]# grep -C 1 leo passwd
test❌502:502::/home/test:/bin/bash
leo❌503:503::/home/leo:/bin/bash
roc❌504:504::/home/roc:/bin/bash

上面示例中使用了-C选项,它是-A和-B选项的合体,表示除了展示匹配行之外,还要展示出匹配行上面和下面各若干行。而示例中的 -C 1 则表示还展示匹配行上面一行和下面一行的内容。
让 grep 不要区分大小写
在搜索过程中,我们有时候希望不要区分字母的大小写,这样做可以提高搜索命中的概率,而-i选项则可以帮我们这个忙。
[root@roclinux ~]# grep -i “LEO” passwd
leo❌503:503::/home/leo:/bin/bash

看到了吧,即便我们把要搜索的字符串指定为全部大写的 LEO,仍然可以顺利地搜索到全部小写的 leo。
grep 处理多文件
grep 命令可以一次搜索很多个文件,最常使用的一个场景就是:从大量的文件中找出含有特定字符的文件。下面我们来试验一下。

当前目录下有三个文件

[roc@roclinux ~]$ ll
total 12
-rw-rw-r-- 1 roc roc 58 Mar 15 17:47 1.txt
-rw-rw-r-- 1 roc roc 59 Mar 15 17:51 2.txt
-rw-rw-r-- 1 roc roc 58 Mar 15 17:52 3.txt

1.txt文件的内容如下

[roc@roclinux ~]$ cat 1.txt
this first file
this file contain some import infomation.

2.txt文件的内容如下

[roc@roclinux ~]$ cat 2.txt
this second file
this file contain some import infomation.

3.txt文件的内容如下

[roc@roclinux ~]$ cat 3.txt
this third file
this file contain some import infomation.

我们的搜索需求是,找出内容中含有first单词的文件都有哪些。我们希望得到的是一个文件列表。

使用-l选项

[roc@roclinux ~]$ grep -l “first” *.txt
1.txt

原来只有 1.txt 文件中包含有 first 单词。如果我想找出不含 first 单词的文件都有哪些,该如何操作呢?
[roc@roclinux ~]$ grep -L “first” *.txt
2.txt
3.txt
反向操作就要用反向选项,只需把 -l 变成 -L 即可。
grep 也正则
上面讲解的内容,更多的是以普通字符串作为搜索对象的,接下来的内容里,我们会更多地设置一些复杂的搜索需求,让大家体会到正则的好处与威力。

我们希望搜索/etc/passwd文件中开头是 leo 的行:

^表示行首

[root@roclinux ~]# grep ‘^leo’ /etc/passwd
leo❌503:503::/home/leo:/bin/bash

看到了吧,我们使用“^leo”表达了“开头是 leo 的行”,而 grep 也心领神会,帮我们找到了所求。

那如果我们希望搜索 /etc/passwd 文件中行尾是 bash 的行呢?

$ 表示行尾

[root@roclinux ~]# grep 'bash ′ / e t c / p a s s w d r o o t : x : 0 : 0 : r o o t : / r o o t : / b i n / b a s h c l o u d − u s e r : x : 500 : 500 : : / h o m e / c l o u d − u s e r : / b i n / b a s h t e s t : x : 502 : 502 : : / h o m e / t e s t : / b i n / b a s h l e o : x : 503 : 503 : : / h o m e / l e o : / b i n / b a s h r o c : x : 504 : 504 : : / h o m e / r o c : / b i n / b a s h 使用“ b a s h ' /etc/passwd root:x:0:0:root:/root:/bin/bash cloud-user:x:500:500::/home/cloud-user:/bin/bash test:x:502:502::/home/test:/bin/bash leo:x:503:503::/home/leo:/bin/bash roc:x:504:504::/home/roc:/bin/bash 使用“bash /etc/passwdroot:x:0:0:root:/root:/bin/bashclouduser:x:500:500::/home/clouduser:/bin/bashtest:x:502:502::/home/test:/bin/bashleo:x:503:503::/home/leo:/bin/bashroc:x:504:504::/home/roc:/bin/bash使用bash”即可,你会发现正则表达式其实并不难。
有关“词”的知识
我们希望搜索含有 bin 这个词的行,按照原来我们学到的知识,一定会这样来搜索:
[root@roclinux ~]# grep bin /etc/passwd
root❌0:0:root:/root:/bin/bash
bin❌1:1:bin:/bin:/sbin/nologin
daemon❌2:2:daemon:/sbin:/sbin/nologin
adm❌3:4:adm:/var/adm:/sbin/nologin
lp❌4:7:lp:/var/spool/lpd:/sbin/nologin
(此处省略数十行)
apache❌48:48:Apache:/var/www:/sbin/nologin
test❌502:502::/home/test:/bin/bash
leo❌503:503::/home/leo:/bin/bash
roc❌504:504::/home/roc:/bin/bash

但是,你会发现,这样的搜索结果有一个问题,那就是连 sbin 这样的词也会被搜索出来,而我们并不想看到这样的结果。这个问题,算是一个比较难的问题了,需要我们了解“词”的定义,以及知道正则表达式中如何表示一个“词”。

首先,正则表达式中的“词(word)”,一般是由字母、数字和下划线所组成的,且词与词之间通常使用空格、制表符或换行符分隔。举个例子,“I love you.”中的“love”就是一个“词”,但“My gloves are red.”中的“love”就不能称作一个“词”。

在正则表达式中,我们通常用尖括号表示一个“词”,比如能够匹配“I love you.”,但不能匹配“My gloves are red.”。

下面我们通过示例学习这个知识点:
#我们尝试匹配bin这个“词”
[root@roclinux ~]# grep ‘<bin>’ /etc/passwd --color
root❌0:0:root:/root:/bin/bash
bin❌1:1:bin:/bin:/sbin/nologin
sync❌5:0:sync:/sbin:/bin/sync
cloud-user❌500:500::/home/cloud-user:/bin/bash
test❌502:502::/home/test:/bin/bash
leo❌503:503::/home/leo:/bin/bash
roc❌504:504::/home/roc:/bin/bash

我们通过使用<和>来准确匹配到了 bin 这个词,而 sbin 却被挡在了门外。

其实,上面的方法还是有些复杂了,grep 中还有一个更加简单的方法来实现对“词”的搜索:

使用-w选项来搜索一个词

[root@roclinux ~]# grep -w ‘bin’ /etc/passwd
root❌0:0:root:/root:/bin/bash
bin❌1:1:bin:/bin:/sbin/nologin
sync❌5:0:sync:/sbin:/bin/sync
cloud-user❌500:500::/home/cloud-user:/bin/bash
test❌502:502::/home/test:/bin/bash
leo❌503:503::/home/leo:/bin/bash
roc❌504:504::/home/roc:/bin/bash
grep 的多条件查找
如果我想查找以 root 为行首的或以 bash 为行尾的行,那应该怎么查找呢?这时就要有请我们的二师兄 egrep 来帮忙了。
[root@roclinux ~]# egrep ‘^root|bash$’ passwd
root❌0:0:root:/root:/bin/bash
cloud-user❌500:500::/home/cloud-user:/bin/bash
test❌502:502::/home/test:/bin/bash
leo❌503:503::/home/leo:/bin/bash
roc❌504:504::/home/roc:/bin/bash

我们通过 egrep 命令实现了两个条件的搜索,其中“|”符号表示“或”,连接了两个搜索条件。

同样的搜索需求,grep 就没有办法满足(除非使用-E选项):
[root@roclinux ~]# grep ‘^root|bash$’ passwd

这就是 grep 和 egrep的区别了,grep 的正则表达式是基本正则表达式,而 egrep 的正则表达式是扩展正则表达式,这两种表达式是有区别的,下面我们就一起来看看它们的区别。
基本正则表达式和扩展正则表达式
世界上的正则表达式种类繁多且复杂,面对这样的状况,POSIX 将正则表达式进行了标准化,并把实现方法分为了两大类:
基本正则表达式(BRE)
扩展正则表达式(ERE)

两者的区别,更多的是元字符的区别。

在基本正则表达式(BRE)中,只承认“^”、“$”、“.”、“[”、“]”、“*”这些是元字符,所有其他的字符都被识别为普通字符。

而在扩展正则表达式(ERE)中,则在BRE的基础上增加了“(”、“)”、“{”、“}”、“?”和“+”、“|”等元字符。

最后要特别说明的一点,只有在用反斜杠进行转义的情况下,字符“(”、“)”、“{”和“}”才会在扩展正则表达式(ERE)中被当作元字符处理,而在基本正则表达式(ERE)中,任何元字符前面加上反斜杠反而会使其被当作普通字符来处理。这样的设计,有些奇葩,同学们一定要记清楚哦。
三师弟 fgrep 最朴素
grep 和 egrep 都支持正则表达式,但 fgrep 却完全不支持正则表达式。
#匹配到了好多行
[root@roclinux ~]# grep -c ‘^root’ /etc/passwd
6688

#一行都没有匹配到
[root@roclinux ~]# fgrep -c ‘^root’ /etc/passwd
0
这个示例证明了 fgrep 的确不支持正则表达式。那 fgrep 还有用武之地么?答案是肯定的。

当我们搜索时,假如搜索字符串中包含了不少特殊字符,而这些特殊字符恰好又是正则表达式预留的字符,比如说“^”、“KaTeX parse error: Expected 'EOF', got '#' at position 77: …通字符。我们来看下面的示例。 #̲我们的roc.txt文件中有几…
[roc@roclinux ~]$ cat roc.txt
^this third file
^$this file contain some import infomation.

grep会尝试去找开头为this的行, 但并未找到

[roc@roclinux ~]$ grep ‘^this’ roc.txt

fgrep会老老实实地去找^this字符串, 它找到了

[roc@roclinux ~]$ fgrep ‘^this’ roc.txt
^this third file
grep 入门招式
grep 是能力最全面的,下面我们就先来学习 grep,围绕 /etc/passwd 文件来讲解 grep 的作用。
[root@roclinux ~]# cat /etc/passwd
root❌0:0:root:/root:/bin/bash
bin❌1:1:bin:/bin:/sbin/nologin
daemon❌2:2:daemon:/sbin:/sbin/nologin
adm❌3:4:adm:/var/adm:/sbin/nologin
lp❌4:7:lp:/var/spool/lpd:/sbin/nologin
(中间省略数十行)
apache❌48:48:Apache:/var/www:/sbin/nologin
test❌502:502::/home/test:/bin/bash
leo❌503:503::/home/leo:/bin/bash
roc❌504:504::/home/roc:/bin/bash
尝试搜索包含leo字符串的行:
[root@roclinux ~]# grep --color “leo” /etc/passwd
leo❌503:503::/home/leo:/bin/bash

注意上面的–color选项,它的作用是高亮我们查找的字符串,这里,leo 字符串变成了红色字体。
grep 的反查技能
下面来搜索不包含 leo 字符串的行。

使用-v选项

[root@roclinux ~]# grep -v “leo” /etc/passwd
root❌0:0:root:/root:/bin/bash
bin❌1:1:bin:/bin:/sbin/nologin
daemon❌2:2:daemon:/sbin:/sbin/nologin
adm❌3:4:adm:/var/adm:/sbin/nologin
lp❌4:7:lp:/var/spool/lpd:/sbin/nologin
(中间省略数十行)
apache❌48:48:Apache:/var/www:/sbin/nologin
test❌502:502::/home/test:/bin/bash
roc❌504:504::/home/roc:/bin/bash

我们使用-v选项实现了反查效果,可以看到,含有 leo 的行都没有展示出来。
grep 展示行号和统计行数
有时,我们希望 grep 不仅能搜索到字符串,还能展示出它们位于文件的第几行,这时我们可以使用-n选项来实现这个效果。

来看看包含 leo 的行位于文件的第几行。

使用选项 n

[root@roclinux ~]# grep -n leo /etc/passwd
29♌️x:503:503::/home/leo:/bin/bash

大家注意观察输出内容的开头部分,leo 前的内容表示的就是行位置信息,原来是在第 29 行呀!

另外一些时候,我们希望 grep 不要输出搜索到的行的内容,而是简单地告诉我们到底搜索到了多少行就好了,试试-c选项吧。

使用-c选项

[root@roclinux ~]# grep -c leo /etc/passwd
1
输出很简单,这表示含有 leo 的行只有 1 行。
grep 能环顾四周
我想搜索包含 leo 的行,但 grep 在输出时,最好能把 leo 所在行的上面或下面相邻的行也都展示出来。

我们来看看 grep 是如何做到的。
[root@roclinux ~]# grep -A 1 leo passwd
leo❌503:503::/home/leo:/bin/bash
roc❌504:504::/home/roc:/bin/bash

上面示例中的-A选项,是 After 的缩写,表示除了展示匹配行之外,还要展示出匹配行下面的若干行。而示例中的 -A 1 则表示还展示匹配行下面一行的内容。
[root@roclinux ~]# grep -B 1 leo passwd
test❌502:502::/home/test:/bin/bash
leo❌503:503::/home/leo:/bin/bash

上面示例中的-B选项,是 Before 的缩写,表示除了展示匹配行之外,还要展示出匹配行上面的若干行。而示例中的 -B 1 则表示还展示匹配行上面一行的内容。
[root@roclinux ~]# grep -C 1 leo passwd
test❌502:502::/home/test:/bin/bash
leo❌503:503::/home/leo:/bin/bash
roc❌504:504::/home/roc:/bin/bash

上面示例中使用了-C选项,它是-A和-B选项的合体,表示除了展示匹配行之外,还要展示出匹配行上面和下面各若干行。而示例中的 -C 1 则表示还展示匹配行上面一行和下面一行的内容。
让 grep 不要区分大小写
在搜索过程中,我们有时候希望不要区分字母的大小写,这样做可以提高搜索命中的概率,而-i选项则可以帮我们这个忙。
[root@roclinux ~]# grep -i “LEO” passwd
leo❌503:503::/home/leo:/bin/bash

看到了吧,即便我们把要搜索的字符串指定为全部大写的 LEO,仍然可以顺利地搜索到全部小写的 leo。
grep 处理多文件
grep 命令可以一次搜索很多个文件,最常使用的一个场景就是:从大量的文件中找出含有特定字符的文件。下面我们来试验一下。

当前目录下有三个文件

[roc@roclinux ~]$ ll
total 12
-rw-rw-r-- 1 roc roc 58 Mar 15 17:47 1.txt
-rw-rw-r-- 1 roc roc 59 Mar 15 17:51 2.txt
-rw-rw-r-- 1 roc roc 58 Mar 15 17:52 3.txt

1.txt文件的内容如下

[roc@roclinux ~]$ cat 1.txt
this first file
this file contain some import infomation.

2.txt文件的内容如下

[roc@roclinux ~]$ cat 2.txt
this second file
this file contain some import infomation.

3.txt文件的内容如下

[roc@roclinux ~]$ cat 3.txt
this third file
this file contain some import infomation.

我们的搜索需求是,找出内容中含有first单词的文件都有哪些。我们希望得到的是一个文件列表。

使用-l选项

[roc@roclinux ~]$ grep -l “first” *.txt
1.txt

原来只有 1.txt 文件中包含有 first 单词。如果我想找出不含 first 单词的文件都有哪些,该如何操作呢?
[roc@roclinux ~]$ grep -L “first” *.txt
2.txt
3.txt
反向操作就要用反向选项,只需把 -l 变成 -L 即可。
grep 也正则
上面讲解的内容,更多的是以普通字符串作为搜索对象的,接下来的内容里,我们会更多地设置一些复杂的搜索需求,让大家体会到正则的好处与威力。

我们希望搜索/etc/passwd文件中开头是 leo 的行:

^表示行首

[root@roclinux ~]# grep ‘^leo’ /etc/passwd
leo❌503:503::/home/leo:/bin/bash

看到了吧,我们使用“^leo”表达了“开头是 leo 的行”,而 grep 也心领神会,帮我们找到了所求。

那如果我们希望搜索 /etc/passwd 文件中行尾是 bash 的行呢?

$ 表示行尾

[root@roclinux ~]# grep 'bash ′ / e t c / p a s s w d r o o t : x : 0 : 0 : r o o t : / r o o t : / b i n / b a s h c l o u d − u s e r : x : 500 : 500 : : / h o m e / c l o u d − u s e r : / b i n / b a s h t e s t : x : 502 : 502 : : / h o m e / t e s t : / b i n / b a s h l e o : x : 503 : 503 : : / h o m e / l e o : / b i n / b a s h r o c : x : 504 : 504 : : / h o m e / r o c : / b i n / b a s h 使用“ b a s h ' /etc/passwd root:x:0:0:root:/root:/bin/bash cloud-user:x:500:500::/home/cloud-user:/bin/bash test:x:502:502::/home/test:/bin/bash leo:x:503:503::/home/leo:/bin/bash roc:x:504:504::/home/roc:/bin/bash 使用“bash /etc/passwdroot:x:0:0:root:/root:/bin/bashclouduser:x:500:500::/home/clouduser:/bin/bashtest:x:502:502::/home/test:/bin/bashleo:x:503:503::/home/leo:/bin/bashroc:x:504:504::/home/roc:/bin/bash使用bash”即可,你会发现正则表达式其实并不难。
有关“词”的知识
我们希望搜索含有 bin 这个词的行,按照原来我们学到的知识,一定会这样来搜索:
[root@roclinux ~]# grep bin /etc/passwd
root❌0:0:root:/root:/bin/bash
bin❌1:1:bin:/bin:/sbin/nologin
daemon❌2:2:daemon:/sbin:/sbin/nologin
adm❌3:4:adm:/var/adm:/sbin/nologin
lp❌4:7:lp:/var/spool/lpd:/sbin/nologin
(此处省略数十行)
apache❌48:48:Apache:/var/www:/sbin/nologin
test❌502:502::/home/test:/bin/bash
leo❌503:503::/home/leo:/bin/bash
roc❌504:504::/home/roc:/bin/bash

但是,你会发现,这样的搜索结果有一个问题,那就是连 sbin 这样的词也会被搜索出来,而我们并不想看到这样的结果。这个问题,算是一个比较难的问题了,需要我们了解“词”的定义,以及知道正则表达式中如何表示一个“词”。

首先,正则表达式中的“词(word)”,一般是由字母、数字和下划线所组成的,且词与词之间通常使用空格、制表符或换行符分隔。举个例子,“I love you.”中的“love”就是一个“词”,但“My gloves are red.”中的“love”就不能称作一个“词”。

在正则表达式中,我们通常用尖括号表示一个“词”,比如能够匹配“I love you.”,但不能匹配“My gloves are red.”。

下面我们通过示例学习这个知识点:
#我们尝试匹配bin这个“词”
[root@roclinux ~]# grep ‘<bin>’ /etc/passwd --color
root❌0:0:root:/root:/bin/bash
bin❌1:1:bin:/bin:/sbin/nologin
sync❌5:0:sync:/sbin:/bin/sync
cloud-user❌500:500::/home/cloud-user:/bin/bash
test❌502:502::/home/test:/bin/bash
leo❌503:503::/home/leo:/bin/bash
roc❌504:504::/home/roc:/bin/bash

我们通过使用<和>来准确匹配到了 bin 这个词,而 sbin 却被挡在了门外。

其实,上面的方法还是有些复杂了,grep 中还有一个更加简单的方法来实现对“词”的搜索:

使用-w选项来搜索一个词

[root@roclinux ~]# grep -w ‘bin’ /etc/passwd
root❌0:0:root:/root:/bin/bash
bin❌1:1:bin:/bin:/sbin/nologin
sync❌5:0:sync:/sbin:/bin/sync
cloud-user❌500:500::/home/cloud-user:/bin/bash
test❌502:502::/home/test:/bin/bash
leo❌503:503::/home/leo:/bin/bash
roc❌504:504::/home/roc:/bin/bash
grep 的多条件查找
如果我想查找以 root 为行首的或以 bash 为行尾的行,那应该怎么查找呢?这时就要有请我们的二师兄 egrep 来帮忙了。
[root@roclinux ~]# egrep ‘^root|bash$’ passwd
root❌0:0:root:/root:/bin/bash
cloud-user❌500:500::/home/cloud-user:/bin/bash
test❌502:502::/home/test:/bin/bash
leo❌503:503::/home/leo:/bin/bash
roc❌504:504::/home/roc:/bin/bash

我们通过 egrep 命令实现了两个条件的搜索,其中“|”符号表示“或”,连接了两个搜索条件。

同样的搜索需求,grep 就没有办法满足(除非使用-E选项):
[root@roclinux ~]# grep ‘^root|bash$’ passwd

这就是 grep 和 egrep的区别了,grep 的正则表达式是基本正则表达式,而 egrep 的正则表达式是扩展正则表达式,这两种表达式是有区别的,下面我们就一起来看看它们的区别。
基本正则表达式和扩展正则表达式
世界上的正则表达式种类繁多且复杂,面对这样的状况,POSIX 将正则表达式进行了标准化,并把实现方法分为了两大类:
基本正则表达式(BRE)
扩展正则表达式(ERE)

两者的区别,更多的是元字符的区别。

在基本正则表达式(BRE)中,只承认“^”、“$”、“.”、“[”、“]”、“*”这些是元字符,所有其他的字符都被识别为普通字符。

而在扩展正则表达式(ERE)中,则在BRE的基础上增加了“(”、“)”、“{”、“}”、“?”和“+”、“|”等元字符。

最后要特别说明的一点,只有在用反斜杠进行转义的情况下,字符“(”、“)”、“{”和“}”才会在扩展正则表达式(ERE)中被当作元字符处理,而在基本正则表达式(ERE)中,任何元字符前面加上反斜杠反而会使其被当作普通字符来处理。这样的设计,有些奇葩,同学们一定要记清楚哦。
三师弟 fgrep 最朴素
grep 和 egrep 都支持正则表达式,但 fgrep 却完全不支持正则表达式。
#匹配到了好多行
[root@roclinux ~]# grep -c ‘^root’ /etc/passwd
6688

#一行都没有匹配到
[root@roclinux ~]# fgrep -c ‘^root’ /etc/passwd
0
这个示例证明了 fgrep 的确不支持正则表达式。那 fgrep 还有用武之地么?答案是肯定的。

当我们搜索时,假如搜索字符串中包含了不少特殊字符,而这些特殊字符恰好又是正则表达式预留的字符,比如说“^”、“KaTeX parse error: Expected 'EOF', got '#' at position 77: …通字符。我们来看下面的示例。 #̲我们的roc.txt文件中有几…
[roc@roclinux ~]$ cat roc.txt
^this third file
^$this file contain some import infomation.

grep会尝试去找开头为this的行, 但并未找到

[roc@roclinux ~]$ grep ‘^this’ roc.txt

fgrep会老老实实地去找^this字符串, 它找到了

[roc@roclinux ~]$ fgrep ‘^this’ roc.txt
^this third file
grep 入门招式
grep 是能力最全面的,下面我们就先来学习 grep,围绕 /etc/passwd 文件来讲解 grep 的作用。
[root@roclinux ~]# cat /etc/passwd
root❌0:0:root:/root:/bin/bash
bin❌1:1:bin:/bin:/sbin/nologin
daemon❌2:2:daemon:/sbin:/sbin/nologin
adm❌3:4:adm:/var/adm:/sbin/nologin
lp❌4:7:lp:/var/spool/lpd:/sbin/nologin
(中间省略数十行)
apache❌48:48:Apache:/var/www:/sbin/nologin
test❌502:502::/home/test:/bin/bash
leo❌503:503::/home/leo:/bin/bash
roc❌504:504::/home/roc:/bin/bash
尝试搜索包含leo字符串的行:
[root@roclinux ~]# grep --color “leo” /etc/passwd
leo❌503:503::/home/leo:/bin/bash

注意上面的–color选项,它的作用是高亮我们查找的字符串,这里,leo 字符串变成了红色字体。
grep 的反查技能
下面来搜索不包含 leo 字符串的行。

使用-v选项

[root@roclinux ~]# grep -v “leo” /etc/passwd
root❌0:0:root:/root:/bin/bash
bin❌1:1:bin:/bin:/sbin/nologin
daemon❌2:2:daemon:/sbin:/sbin/nologin
adm❌3:4:adm:/var/adm:/sbin/nologin
lp❌4:7:lp:/var/spool/lpd:/sbin/nologin
(中间省略数十行)
apache❌48:48:Apache:/var/www:/sbin/nologin
test❌502:502::/home/test:/bin/bash
roc❌504:504::/home/roc:/bin/bash

我们使用-v选项实现了反查效果,可以看到,含有 leo 的行都没有展示出来。
grep 展示行号和统计行数
有时,我们希望 grep 不仅能搜索到字符串,还能展示出它们位于文件的第几行,这时我们可以使用-n选项来实现这个效果。

来看看包含 leo 的行位于文件的第几行。

使用选项 n

[root@roclinux ~]# grep -n leo /etc/passwd
29♌️x:503:503::/home/leo:/bin/bash

大家注意观察输出内容的开头部分,leo 前的内容表示的就是行位置信息,原来是在第 29 行呀!

另外一些时候,我们希望 grep 不要输出搜索到的行的内容,而是简单地告诉我们到底搜索到了多少行就好了,试试-c选项吧。

使用-c选项

[root@roclinux ~]# grep -c leo /etc/passwd
1
输出很简单,这表示含有 leo 的行只有 1 行。
grep 能环顾四周
我想搜索包含 leo 的行,但 grep 在输出时,最好能把 leo 所在行的上面或下面相邻的行也都展示出来。

我们来看看 grep 是如何做到的。
[root@roclinux ~]# grep -A 1 leo passwd
leo❌503:503::/home/leo:/bin/bash
roc❌504:504::/home/roc:/bin/bash

上面示例中的-A选项,是 After 的缩写,表示除了展示匹配行之外,还要展示出匹配行下面的若干行。而示例中的 -A 1 则表示还展示匹配行下面一行的内容。
[root@roclinux ~]# grep -B 1 leo passwd
test❌502:502::/home/test:/bin/bash
leo❌503:503::/home/leo:/bin/bash

上面示例中的-B选项,是 Before 的缩写,表示除了展示匹配行之外,还要展示出匹配行上面的若干行。而示例中的 -B 1 则表示还展示匹配行上面一行的内容。
[root@roclinux ~]# grep -C 1 leo passwd
test❌502:502::/home/test:/bin/bash
leo❌503:503::/home/leo:/bin/bash
roc❌504:504::/home/roc:/bin/bash

上面示例中使用了-C选项,它是-A和-B选项的合体,表示除了展示匹配行之外,还要展示出匹配行上面和下面各若干行。而示例中的 -C 1 则表示还展示匹配行上面一行和下面一行的内容。
让 grep 不要区分大小写
在搜索过程中,我们有时候希望不要区分字母的大小写,这样做可以提高搜索命中的概率,而-i选项则可以帮我们这个忙。
[root@roclinux ~]# grep -i “LEO” passwd
leo❌503:503::/home/leo:/bin/bash

看到了吧,即便我们把要搜索的字符串指定为全部大写的 LEO,仍然可以顺利地搜索到全部小写的 leo。
grep 处理多文件
grep 命令可以一次搜索很多个文件,最常使用的一个场景就是:从大量的文件中找出含有特定字符的文件。下面我们来试验一下。

当前目录下有三个文件

[roc@roclinux ~]$ ll
total 12
-rw-rw-r-- 1 roc roc 58 Mar 15 17:47 1.txt
-rw-rw-r-- 1 roc roc 59 Mar 15 17:51 2.txt
-rw-rw-r-- 1 roc roc 58 Mar 15 17:52 3.txt

1.txt文件的内容如下

[roc@roclinux ~]$ cat 1.txt
this first file
this file contain some import infomation.

2.txt文件的内容如下

[roc@roclinux ~]$ cat 2.txt
this second file
this file contain some import infomation.

3.txt文件的内容如下

[roc@roclinux ~]$ cat 3.txt
this third file
this file contain some import infomation.

我们的搜索需求是,找出内容中含有first单词的文件都有哪些。我们希望得到的是一个文件列表。

使用-l选项

[roc@roclinux ~]$ grep -l “first” *.txt
1.txt

原来只有 1.txt 文件中包含有 first 单词。如果我想找出不含 first 单词的文件都有哪些,该如何操作呢?
[roc@roclinux ~]$ grep -L “first” *.txt
2.txt
3.txt
反向操作就要用反向选项,只需把 -l 变成 -L 即可。
grep 也正则
上面讲解的内容,更多的是以普通字符串作为搜索对象的,接下来的内容里,我们会更多地设置一些复杂的搜索需求,让大家体会到正则的好处与威力。

我们希望搜索/etc/passwd文件中开头是 leo 的行:

^表示行首

[root@roclinux ~]# grep ‘^leo’ /etc/passwd
leo❌503:503::/home/leo:/bin/bash

看到了吧,我们使用“^leo”表达了“开头是 leo 的行”,而 grep 也心领神会,帮我们找到了所求。

那如果我们希望搜索 /etc/passwd 文件中行尾是 bash 的行呢?

$ 表示行尾

[root@roclinux ~]# grep 'bash ′ / e t c / p a s s w d r o o t : x : 0 : 0 : r o o t : / r o o t : / b i n / b a s h c l o u d − u s e r : x : 500 : 500 : : / h o m e / c l o u d − u s e r : / b i n / b a s h t e s t : x : 502 : 502 : : / h o m e / t e s t : / b i n / b a s h l e o : x : 503 : 503 : : / h o m e / l e o : / b i n / b a s h r o c : x : 504 : 504 : : / h o m e / r o c : / b i n / b a s h 使用“ b a s h ' /etc/passwd root:x:0:0:root:/root:/bin/bash cloud-user:x:500:500::/home/cloud-user:/bin/bash test:x:502:502::/home/test:/bin/bash leo:x:503:503::/home/leo:/bin/bash roc:x:504:504::/home/roc:/bin/bash 使用“bash /etc/passwdroot:x:0:0:root:/root:/bin/bashclouduser:x:500:500::/home/clouduser:/bin/bashtest:x:502:502::/home/test:/bin/bashleo:x:503:503::/home/leo:/bin/bashroc:x:504:504::/home/roc:/bin/bash使用bash”即可,你会发现正则表达式其实并不难。
有关“词”的知识
我们希望搜索含有 bin 这个词的行,按照原来我们学到的知识,一定会这样来搜索:
[root@roclinux ~]# grep bin /etc/passwd
root❌0:0:root:/root:/bin/bash
bin❌1:1:bin:/bin:/sbin/nologin
daemon❌2:2:daemon:/sbin:/sbin/nologin
adm❌3:4:adm:/var/adm:/sbin/nologin
lp❌4:7:lp:/var/spool/lpd:/sbin/nologin
(此处省略数十行)
apache❌48:48:Apache:/var/www:/sbin/nologin
test❌502:502::/home/test:/bin/bash
leo❌503:503::/home/leo:/bin/bash
roc❌504:504::/home/roc:/bin/bash

但是,你会发现,这样的搜索结果有一个问题,那就是连 sbin 这样的词也会被搜索出来,而我们并不想看到这样的结果。这个问题,算是一个比较难的问题了,需要我们了解“词”的定义,以及知道正则表达式中如何表示一个“词”。

首先,正则表达式中的“词(word)”,一般是由字母、数字和下划线所组成的,且词与词之间通常使用空格、制表符或换行符分隔。举个例子,“I love you.”中的“love”就是一个“词”,但“My gloves are red.”中的“love”就不能称作一个“词”。

在正则表达式中,我们通常用尖括号表示一个“词”,比如能够匹配“I love you.”,但不能匹配“My gloves are red.”。

下面我们通过示例学习这个知识点:
#我们尝试匹配bin这个“词”
[root@roclinux ~]# grep ‘<bin>’ /etc/passwd --color
root❌0:0:root:/root:/bin/bash
bin❌1:1:bin:/bin:/sbin/nologin
sync❌5:0:sync:/sbin:/bin/sync
cloud-user❌500:500::/home/cloud-user:/bin/bash
test❌502:502::/home/test:/bin/bash
leo❌503:503::/home/leo:/bin/bash
roc❌504:504::/home/roc:/bin/bash

我们通过使用<和>来准确匹配到了 bin 这个词,而 sbin 却被挡在了门外。

其实,上面的方法还是有些复杂了,grep 中还有一个更加简单的方法来实现对“词”的搜索:

使用-w选项来搜索一个词

[root@roclinux ~]# grep -w ‘bin’ /etc/passwd
root❌0:0:root:/root:/bin/bash
bin❌1:1:bin:/bin:/sbin/nologin
sync❌5:0:sync:/sbin:/bin/sync
cloud-user❌500:500::/home/cloud-user:/bin/bash
test❌502:502::/home/test:/bin/bash
leo❌503:503::/home/leo:/bin/bash
roc❌504:504::/home/roc:/bin/bash
grep 的多条件查找
如果我想查找以 root 为行首的或以 bash 为行尾的行,那应该怎么查找呢?这时就要有请我们的二师兄 egrep 来帮忙了。
[root@roclinux ~]# egrep ‘^root|bash$’ passwd
root❌0:0:root:/root:/bin/bash
cloud-user❌500:500::/home/cloud-user:/bin/bash
test❌502:502::/home/test:/bin/bash
leo❌503:503::/home/leo:/bin/bash
roc❌504:504::/home/roc:/bin/bash

我们通过 egrep 命令实现了两个条件的搜索,其中“|”符号表示“或”,连接了两个搜索条件。

同样的搜索需求,grep 就没有办法满足(除非使用-E选项):
[root@roclinux ~]# grep ‘^root|bash$’ passwd

这就是 grep 和 egrep的区别了,grep 的正则表达式是基本正则表达式,而 egrep 的正则表达式是扩展正则表达式,这两种表达式是有区别的,下面我们就一起来看看它们的区别。
基本正则表达式和扩展正则表达式
世界上的正则表达式种类繁多且复杂,面对这样的状况,POSIX 将正则表达式进行了标准化,并把实现方法分为了两大类:
基本正则表达式(BRE)
扩展正则表达式(ERE)

两者的区别,更多的是元字符的区别。

在基本正则表达式(BRE)中,只承认“^”、“$”、“.”、“[”、“]”、“*”这些是元字符,所有其他的字符都被识别为普通字符。

而在扩展正则表达式(ERE)中,则在BRE的基础上增加了“(”、“)”、“{”、“}”、“?”和“+”、“|”等元字符。

最后要特别说明的一点,只有在用反斜杠进行转义的情况下,字符“(”、“)”、“{”和“}”才会在扩展正则表达式(ERE)中被当作元字符处理,而在基本正则表达式(ERE)中,任何元字符前面加上反斜杠反而会使其被当作普通字符来处理。这样的设计,有些奇葩,同学们一定要记清楚哦。
三师弟 fgrep 最朴素
grep 和 egrep 都支持正则表达式,但 fgrep 却完全不支持正则表达式。
#匹配到了好多行
[root@roclinux ~]# grep -c ‘^root’ /etc/passwd
6688

#一行都没有匹配到
[root@roclinux ~]# fgrep -c ‘^root’ /etc/passwd
0
这个示例证明了 fgrep 的确不支持正则表达式。那 fgrep 还有用武之地么?答案是肯定的。

当我们搜索时,假如搜索字符串中包含了不少特殊字符,而这些特殊字符恰好又是正则表达式预留的字符,比如说“^”、“KaTeX parse error: Expected 'EOF', got '#' at position 77: …通字符。我们来看下面的示例。 #̲我们的roc.txt文件中有几…
[roc@roclinux ~]$ cat roc.txt
^this third file
^$this file contain some import infomation.

grep会尝试去找开头为this的行, 但并未找到

[roc@roclinux ~]$ grep ‘^this’ roc.txt

fgrep会老老实实地去找^this字符串, 它找到了

[roc@roclinux ~]$ fgrep ‘^this’ roc.txt
^this third file

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值