命 令 | 是否支持正则 | 支持的正则类型 |
---|---|---|
grep | √ | 基本正则表达式 |
egrep | √ | 扩展正则表达式 |
fgrep | × |
[root@roclinux ~]# cat /etc/passwdroot:x:0:0:root:/root:/bin/bashbin:x:1:1:bin:/bin:/sbin/nologindaemon:x:2:2:daemon:/sbin:/sbin/nologinadm:x:3:4:adm:/var/adm:/sbin/nologinlp:x:4:7:lp:/var/spool/lpd:/sbin/nologin(中间省略数十行)apache:x:48:48:Apache:/var/www:/sbin/nologintest:x:502:502::/home/test:/bin/bashleo:x:503:503::/home/leo:/bin/bashroc:x:504:504::/home/roc:/bin/bash尝试搜索包含leo字符串的行:[root@roclinux ~]# grep --color "leo" /etc/passwdleo:x:503:503::/home/leo:/bin/bash
--color
选项,它的作用是高亮我们查找的字符串,这里,leo 字符串变成了红色字体。# 使用-v选项[root@roclinux ~]# grep -v "leo" /etc/passwdroot:x:0:0:root:/root:/bin/bashbin:x:1:1:bin:/bin:/sbin/nologindaemon:x:2:2:daemon:/sbin:/sbin/nologinadm:x:3:4:adm:/var/adm:/sbin/nologinlp:x:4:7:lp:/var/spool/lpd:/sbin/nologin(中间省略数十行)apache:x:48:48:Apache:/var/www:/sbin/nologintest:x:502:502::/home/test:/bin/bashroc:x:504:504::/home/roc:/bin/bash
-v
选项实现了反查效果,可以看到,含有 leo 的行都没有展示出来。-n
选项来实现这个效果。# 使用选项 n[root@roclinux ~]# grep -n leo /etc/passwd29:leo:x:503:503::/home/leo:/bin/bash
-c
选项吧。# 使用-c选项[root@roclinux ~]# grep -c leo /etc/passwd1输出很简单,这表示含有 leo 的行只有 1 行。
[root@roclinux ~]# grep -A 1 leo passwdleo:x:503:503::/home/leo:/bin/bashroc:x:504:504::/home/roc:/bin/bash
-A
选项,是 After 的缩写,表示除了展示匹配行之外,还要展示出匹配行下面的若干行。而示例中的 -A 1 则表示还展示匹配行下面一行的内容。[root@roclinux ~]# grep -B 1 leo passwdtest:x:502:502::/home/test:/bin/bashleo:x:503:503::/home/leo:/bin/bash
-B
选项,是 Before 的缩写,表示除了展示匹配行之外,还要展示出匹配行上面的若干行。而示例中的 -B 1 则表示还展示匹配行上面一行的内容。[root@roclinux ~]# grep -C 1 leo passwdtest:x:502:502::/home/test:/bin/bashleo:x:503:503::/home/leo:/bin/bashroc:x:504:504::/home/roc:/bin/bash
-C
选项,它是-A
和-B
选项的合体,表示除了展示匹配行之外,还要展示出匹配行上面和下面各若干行。而示例中的 -C 1 则表示还展示匹配行上面一行和下面一行的内容。[root@roclinux ~]# grep -i "LEO" passwdleo:x:503:503::/home/leo:/bin/bash
# 当前目录下有三个文件[roc@roclinux ~]$ lltotal 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.txtthis first filethis file contain some import infomation. # 2.txt文件的内容如下[roc@roclinux ~]$ cat 2.txtthis second filethis file contain some import infomation. # 3.txt文件的内容如下[roc@roclinux ~]$ cat 3.txtthis third filethis file contain some import infomation.
# 使用-l选项[roc@roclinux ~]$ grep -l "first" *.txt1.txt
[roc@roclinux ~]$ grep -L "first" *.txt2.txt3.txt反向操作就要用反向选项,只需把 -l 变成 -L 即可。
# ^表示行首[root@roclinux ~]# grep '^leo' /etc/passwdleo:x:503:503::/home/leo:/bin/bash
# $ 表示行尾[root@roclinux ~]# grep 'bash$' /etc/passwdroot:x:0:0:root:/root:/bin/bashcloud-user:x:500:500::/home/cloud-user:/bin/bashtest:x:502:502::/home/test:/bin/bashleo:x:503:503::/home/leo:/bin/bashroc:x:504:504::/home/roc:/bin/bash使用“bash$”即可,你会发现正则表达式其实并不难。
[root@roclinux ~]# grep bin /etc/passwdroot:x:0:0:root:/root:/bin/bashbin:x:1:1:bin:/bin:/sbin/nologindaemon:x:2:2:daemon:/sbin:/sbin/nologinadm:x:3:4:adm:/var/adm:/sbin/nologinlp:x:4:7:lp:/var/spool/lpd:/sbin/nologin(此处省略数十行)apache:x:48:48:Apache:/var/www:/sbin/nologintest:x:502:502::/home/test:/bin/bashleo:x:503:503::/home/leo:/bin/bashroc:x:504:504::/home/roc:/bin/bash
#我们尝试匹配bin这个“词”[root@roclinux ~]# grep '\<bin\>' /etc/passwd --colorroot:x:0:0:root:/root:/bin/bashbin:x:1:1:bin:/bin:/sbin/nologinsync:x:5:0:sync:/sbin:/bin/synccloud-user:x:500:500::/home/cloud-user:/bin/bashtest:x:502:502::/home/test:/bin/bashleo:x:503:503::/home/leo:/bin/bashroc:x:504:504::/home/roc:/bin/bash
\<
和\>
来准确匹配到了 bin 这个词,而 sbin 却被挡在了门外。# 使用-w选项来搜索一个词[root@roclinux ~]# grep -w 'bin' /etc/passwdroot:x:0:0:root:/root:/bin/bashbin:x:1:1:bin:/bin:/sbin/nologinsync:x:5:0:sync:/sbin:/bin/synccloud-user:x:500:500::/home/cloud-user:/bin/bashtest:x:502:502::/home/test:/bin/bashleo:x:503:503::/home/leo:/bin/bashroc:x:504:504::/home/roc:/bin/bash
[root@roclinux ~]# egrep '^root|bash$' passwdroot:x:0:0:root:/root:/bin/bashcloud-user:x:500:500::/home/cloud-user:/bin/bashtest:x:502:502::/home/test:/bin/bashleo:x:503:503::/home/leo:/bin/bashroc:x:504:504::/home/roc:/bin/bash
[root@roclinux ~]# grep '^root|bash$' passwd
#匹配到了好多行[root@roclinux ~]# grep -c '^root' /etc/passwd6688 #一行都没有匹配到[root@roclinux ~]# fgrep -c '^root' /etc/passwd0这个示例证明了 fgrep 的确不支持正则表达式。那 fgrep 还有用武之地么?答案是肯定的。
#我们的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
联系客服