grep

发布时间 2023-12-07 10:31:46作者: 惠恒博

基本概述

grep命令来自英文词组global search regular expression and print out the line的缩写,意思是用于全面搜索的正则表达式,并将结果输出。人们通常会将grep命令与正则表达式搭配使用,参数作为搜索过程中的补充或对输出结果的筛选,命令模式十分灵‍活。

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

语法格式

grep 参数 文件名

grep   [参数]     '字符串'     文本文件...

常用参数

-E:==egrep 支持扩展正则

-A:匹配内容的下面n行 after -A5

-B:匹配内容的上面n行

-b:显示匹配行距文件头部的偏移量

-C:匹配内容的匹配上下n行

-c:显示匹配的数,类似于wc -l

-v:取反,排除;显示不包含匹配文本的所有行

-n:显示所有匹配行及其行号

-i:忽略关键词大小写

-r:递归搜索模式

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

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

-w:匹配整词

-h:搜索多文件时不显示文件名

-l:只显示符合匹配条件的文件名

-x:精准匹配整行

-o:测试,显示匹配词距文件头部的偏移量

-q:表示 quiet(静默),结合此选项可以只做检索而并不输出,使用 -q 选项的效果与使用 &> /dev/null的效果类似

^word:以字符串word开头

word$:以字符串word结尾

^$:匹配空行

参考示例

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

[root@linuxcool ~]# grep root /etc/passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
]# grep  bin     /etc/passwd    #找有bin的行
]# grep  bash$   /etc/passwd    #找以bash结尾的行

2.搜索指定文件中以某个关键词开头的内容行

[root@linuxcool ~]# grep ^root /etc/passwd 
root:x:0:0:root:/root:/bin/bash 
]# grep  ^bin    /etc/passwd    #找以bin开头的行

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=www.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为不包含)

[root@linuxcool ~]# grep -q linuxprobe anaconda-ks.cfg | echo $?
0
[root@linuxcool ~]# grep -q linuxcool anaconda-ks.cfg | echo $?
1
[root@linuxcool ~]# grep -q '^192.168.4.4' /etc/hosts && echo "YES" || echo "NO"
YES

13.搜索指定文件中空行的数量

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

14.不显示指定文件中的空行

[root@linuxcool ~]# grep  -v  ^$   /etc/default/useradd

15.不显示指定文件中的空行和带井号(#)的行

[root@linuxcool ~]# grep  -v  ^#  /etc/default/useradd  #在Linux系统中,大多数配置文件以#开头的行,为注释行
[root@linuxcool ~]# grep  -v  ^#  /etc/default/useradd | grep   -v  ^$   #显示有效配置(去除空行与注释行)
[root@linuxcool ~]# grep  -v  ^#  /etc/default/useradd  | grep   -v   ^$  >  /opt/nsd03.txt  #显示不要空行和#号的行
[root@linuxcool ~]# grep  -v  ^#  /etc/login.defs | grep  -v ^$  >  /opt/log.txt  #显示不要空行和#号的行

16.搜索指定关键词并忽略大小写

[root@linuxcool ~]# grep  -i  man   /etc/man_db.conf   #忽略大小写

17.利用特殊符号来指定关键词查找

[root@linuxcool ~]# grep  "[root]" user.txt   #找root四个字符任意一个
[root@linuxcool ~]# grep  "[a-z]" user.txt    #找所有小写字母
[root@linuxcool ~]# grep  "[A-Z]" user.txt    #找所有大写字母
[root@linuxcool ~]# grep  "[0-9]" user.txt    #找所有数字
[root@linuxcool ~]# grep  "[^a-Z]" user.txt   #找字母之外的内容,^写在[]里是取反效果
[root@linuxcool ~]# grep  "r..t" user   #找rt之间有2个任意字符的行
[root@linuxcool ~]# grep  "ro*t" user   #找rt,中间的o有没有都行
[root@linuxcool ~]# grep  "."  user     #找任意单个字符
[root@linuxcool ~]# grep  ".*" user     #找任意   
[root@linuxcool ~]# grep  "ro\{2,4\}t" user  #找rt中间的o可以是2~4个
[root@linuxcool ~]# grep  "ro\{3,\}t" user   #找rt中间的o可以是3个以及3个以上
[root@linuxcool ~]# grep  "ro\{2\}t" user    #找rt中间的o必须是2个