【Linux】一步一步学Linux——grep命令(49)

00. 目录

01. 命令概述

Linux系统中grep命令是一种强大的文本搜索工具,它能使用正则表达式搜索文本,并把匹 配的行打印出来。grep全称是Global Regular Expression Print,表示全局正则表达式版本,它的使用权限是所有用户。

grep命令的选项用于对搜索过程的补充,而其命令的模式十分灵活,可以是变量、字符串、正则表达式。需要注意的是:一当模式中包含了空格,务必要用双引号将其引起来。

linux系统支持三种形式的grep命令,大儿子就是grep,标准,模仿的代表。二儿子兴趣爱好多-egrep,简称扩展grep命令,其实和grep -E等价,支持基本和扩展的正则表达式。小儿子跑的最快-fgrep,简称快速grep命令,其实和grep -F等价,不支持正则表达式,按照字符串表面意思进行匹配。

02. 命令格式

grep [options] PATTERN [FILE...]
grep [options] [-e PATTERN | -f FILE] [FILE...]

03. 常用选项

匹配模式选择:
 -E, --extended-regexp     扩展正则表达式egrep
 -F, --fixed-strings       一个换行符分隔的字符串的集合fgrep
 -G, --basic-regexp        基本正则
 -P, --perl-regexp         调用的perl正则
 -e, --regexp=PATTERN      后面根正则模式,默认无
 -f, --file=FILE           从文件中获得匹配模式
 -i, --ignore-case         不区分大小写
 -w, --word-regexp         匹配整个单词
 -x, --line-regexp         匹配整行
 -z, --null-data           一个 0 字节的数据行,但不是空行

杂项:
 -s, --no-messages         不显示错误信息
 -v, --invert-match        显示不匹配的行
 -V, --version             显示版本号
 --help                    显示帮助信息
 --mmap                use memory-mapped input if possible

输入控制:
 -m, --max-count=NUM       匹配的最大数
 -b, --byte-offset         打印匹配行前面打印该行所在的块号码。
 -n, --line-number         显示的加上匹配所在的行号
 --line-buffered           刷新输出每一行
 -H, --with-filename       当搜索多个文件时,显示匹配文件名前缀
 -h, --no-filename         当搜索多个文件时,不显示匹配文件名前缀
 --label=LABEL            print LABEL as filename for standard input
 -o, --only-matching       只显示一行中匹配PATTERN 的部分
 -q, --quiet, --silent      不显示任何东西
 --binary-files=TYPE   假定二进制文件的TYPE 类型;
                                      TYPE 可以是`binary', `text', 或`without-match'
 -a, --text                匹配二进制的东西
 -I                        不匹配二进制的东西
 -d, --directories=ACTION  目录操作,读取,递归,跳过
 -D, --devices=ACTION      设置对设备,FIFO,管道的操作,读取,跳过
 -R, -r, --recursive       递归调用
 --include=PATTERN     只查找匹配FILE_PATTERN 的文件
 --exclude=PATTERN     跳过匹配FILE_PATTERN 的文件和目录
 --exclude-from=FILE   跳过所有除FILE 以外的文件
 -L, --files-without-match 匹配多个文件时,显示不匹配的文件名
 -l, --files-with-matches  匹配多个文件时,显示匹配的文件名
 -c, --count               显示匹配的行数
 -Z, --null                在FILE 文件最后打印空字符

文件控制:
 -B, --before-context=NUM  打印匹配本身以及前面的几个行由NUM控制
 -A, --after-context=NUM   打印匹配本身以及随后的几个行由NUM控制
 -C, --context=NUM         打印匹配本身以及随后,前面的几个行由NUM控制
 -NUM                      根-C的用法一样的
 --color[=WHEN],
 --colour[=WHEN]       使用标志高亮匹配字串;
 
 -U, --binary               使用标志高亮匹配字串;
 -u, --unix-byte-offsets   当CR 字符不存在,报告字节偏移(MSDOS 模式)
 
规则表达式:
grep的规则表达式:

^  #锚定行的开始 如:'^grep'匹配所有以grep开头的行。    
$  #锚定行的结束 如:'grep$'匹配所有以grep结尾的行。    
.  #匹配一个非换行符的字符 如:'gr.p'匹配gr后接一个任意字符,然后是p。    
*  #匹配零个或多个先前字符 如:'*grep'匹配所有一个或多个空格后紧跟grep的行。    
.*   #一起用代表任意字符。   
[]   #匹配一个指定范围内的字符,如'[Gg]rep'匹配Grep和grep。    
[^]  #匹配一个不在指定范围内的字符,如:'[^A-FH-Z]rep'匹配不包含A-R和T-Z的一个字母开头,紧跟rep的行。    
\(..\)  #标记匹配字符,如'\(love\)',love被标记为1。    
\<      #锚定单词的开始,如:'\<grep'匹配包含以grep开头的单词的行。    
\>      #锚定单词的结束,如'grep\>'匹配包含以grep结尾的单词的行。    
x\{m\}  #重复字符x,m次,如:'0\{5\}'匹配包含5个o的行。    
x\{m,\}  #重复字符x,至少m次,如:'o\{5,\}'匹配至少有5个o的行。    
x\{m,n\}  #重复字符x,至少m次,不多于n次,如:'o\{5,10\}'匹配5--10个o的行。   
\w    #匹配文字和数字字符,也就是[A-Za-z0-9],如:'G\w*p'匹配以G后跟零个或多个文字或数字字符,然后是p。   
\W    #\w的反置形式,匹配一个或多个非单词字符,如点号句号等。   
\b    #单词锁定符,如: '\bgrep\b'只匹配grep。  

POSIX字符:
为了在不同国家的字符编码中保持一至,POSIX(The Portable Operating System Interface)增加了特殊的字符类,如[:alnum:][A-Za-z0-9]的另一个写法。要把它们放到[]号内才能成为正则表达式,如[A- Za-z0-9][[:alnum:]]。在linux下的grep除fgrep外,都支持POSIX的字符类。

[:alnum:]    #文字数字字符   
[:alpha:]    #文字字符   
[:digit:]    #数字字符   
[:graph:]    #非空字符(非空格、控制字符)   
[:lower:]    #小写字符   
[:cntrl:]    #控制字符   
[:print:]    #非空字符(包括空格)   
[:punct:]    #标点符号   
[:space:]    #所有空白字符(新行,空格,制表符)   
[:upper:]    #大写字符   
[:xdigit:]   #十六进制数字(0-9,a-f,A-F)  

04. 参考示例

4.1 文件中搜索一个单词

[deng@localhost share]$ grep "root" /etc/passwd 
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
[deng@localhost share]$ 

4.2 多文件中查找单词

[deng@localhost test]$ grep "root" passwd passwd1
passwd:root:x:0:0:root:/root:/bin/bash
passwd:operator:x:11:0:operator:/root:/sbin/nologin
passwd1:root:x:0:0:root:/root:/bin/bash
passwd1:operator:x:11:0:operator:/root:/sbin/nologin
[deng@localhost test]$ 

4.3 输出除匹配之外的所有行

[deng@localhost test]$ grep -v "root" /etc/passwd

4.4 匹配部分颜色显示

[deng@localhost test]$ grep "root" /etc/passwd --color=auto
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
[deng@localhost test]$ 

4.5 使用正则表达式

[deng@localhost test]$ grep -E "[1-9]+" /etc/passwd

或者

[deng@localhost test]$ egrep "[1-9]+" /etc/passwd

4.6 只输出文件中匹配到的部分

[deng@localhost test]$ grep -o "root" /etc/passwd
root
root
root
root
[deng@localhost test]$ 

4.7 统计文件或者文本中包含匹配字符串的行数

[deng@localhost test]$ grep -c "root" /etc/passwd
2

4.8 输出包含匹配字符串的行号

[deng@localhost test]$ grep -n "root" /etc/passwd 
1:root:x:0:0:root:/root:/bin/bash
10:operator:x:11:0:operator:/root:/sbin/nologin
[deng@localhost test]$ 

多个文件匹配情况

[deng@localhost test]$ grep -n "root" passwd passwd1
passwd:1:root:x:0:0:root:/root:/bin/bash
passwd:10:operator:x:11:0:operator:/root:/sbin/nologin
passwd1:1:root:x:0:0:root:/root:/bin/bash
passwd1:10:operator:x:11:0:operator:/root:/sbin/nologin
[deng@localhost test]$ 

4.9 输出字符或字节偏移

-b显示字符或者字节偏移

[deng@localhost test]$ grep -b -o "root" /etc/passwd
0:root
11:root
17:root
366:root
[deng@localhost test]$ 

4.10 搜索多个文件并输出查找匹配文本文件

[deng@localhost test]$ grep -l "root" passwd passwd1
passwd
passwd1
[deng@localhost test]$ 

4.11 在多级目录中对文本进行递归搜索

[deng@localhost test]$ grep -r "root" /etc

4.12 忽略大小写搜索

[deng@localhost test]$ grep -i "ROOT" /etc/passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
[deng@localhost test]$ 

4.13 同时指定多个匹配的字符串

[deng@localhost test]$ grep -e "root" -e "deng" /etc/passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
deng:x:1000:1000:deng:/home/deng:/bin/bash
[deng@localhost test]$ 

4.14 在grep搜索结果中包括或者排除指定文件

在目录中所有的.php和.html文件中递归搜索字符"main"

[deng@localhost test]$ grep -r "main" ./ --include *.{php,html}

搜索结果中排除所有README文件

[deng@localhost test]$ grep -r "main" ./ --exclude "README"

搜索结果中排除filelist文件列表里的文件

[deng@localhost test]$ grep -r "main" ./ --exclude-from filelist

4.15 不显示任何东西

[deng@localhost test]$ grep -q "root" /etc/passwd

4.16 使用0值字节后缀的grep与xargs

#测试文件:
echo "aaa" > file1
echo "bbb" > file2
echo "aaa" > file3

grep "aaa" file* -lZ | xargs -0 rm

#执行后会删除file1和file3,grep输出用-Z选项来指定以0值字节作为终结符文件名(\0),xargs -0 读取输入并用0值字节终结符分隔文件名,然后删除匹配文件,-Z通常和-l结合使用。

4.17 打印出匹配文本之前或者之后的行

显示匹配某个结果之后的3行,使用 -A 选项

[deng@localhost test]$ seq 1 10 | grep "5" -A 3
5
6
7
8
[deng@localhost test]$ 

显示匹配某个结果之前的3行,使用 -B 选项

[deng@localhost test]$ seq 1 10 | grep "5" -B 3
2
3
4
5
[deng@localhost test]$ 

显示匹配某个结果的前三行和后三行,使用 -C 选项

[deng@localhost test]$ seq 1 10 | grep "5" -C 3
2
3
4
5
6
7
8
[deng@localhost test]$ 

匹配结果有多个,会用“–”作为各匹配结果之间的分隔符

[deng@localhost test]$ echo -e "a\nb\nc\na\nb\nc" | grep a -A 1
a
b
--
a
b

4.18 查找一个文件中的空行和非空行

统计文件中空行的行数

[deng@localhost test]$ grep -c ^$ /etc/passwd

统计文件中非空行的行数

[deng@localhost test]$ grep -c ^[^$] /etc/passwd
46
[deng@localhost test]$ 

4.19 输出所有以root开头的行

[deng@localhost test]$ grep ^root /etc/passwd
root:x:0:0:root:/root:/bin/bash
[deng@localhost test]$ 

4.20 输出所有以bash结尾的行

[deng@localhost test]$ grep bash$ /etc/passwd
root:x:0:0:root:/root:/bin/bash
deng:x:1000:1000:deng:/home/deng:/bin/bash
oracle:x:1001:1002::/home/oracle:/bin/bash
itcast:x:1002:1003::/home/itcast:/bin/bash
[deng@localhost test]$ 

4.21 从文件中读取关键字搜索

[deng@localhost test]$ grep -f file /etc/passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
[deng@localhost test]$ cat file 
root
[deng@localhost test]$ 

说明:输出/etc/passwd文件中含有从file文件中读取出的关键词的内容行

4.22 支持正则表达式

显示当前目录下面以.txt 结尾的文件中的所有包含每个字符串至少有7个连续小写字符的字符串的行

[deng@localhost test]$ grep '[a-z]\{7\}'  *.txt

4.23 显示所有匹配root和deng的行

[deng@localhost test]$ grep -E "root|deng" /etc/passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
deng:x:1000:1000:deng:/home/deng:/bin/bash
[deng@localhost test]$ 

4.24 匹配IP地址

X\{m,n\} 匹配字符X m—n 次

[deng@localhost test]$ ifconfig ens33 |grep "[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}"
        inet 192.168.1.104  netmask 255.255.255.0  broadcast 192.168.1.255
[deng@localhost test]$ 

4.25 匹配root或者deng或者itcast

[deng@localhost test]$ grep '\(root\|deng\|itcast\)' /etc/passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
deng:x:1000:1000:deng:/home/deng:/bin/bash
itcast:x:1002:1003::/home/itcast:/bin/bash
[deng@localhost test]$ 

4.26 X\{m,\} 匹配字符X 最少m次

[deng@localhost test]$ grep 'o\{2,\}' /etc/passwd
root:x:0:0:root:/root:/bin/bash
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
setroubleshoot:x:994:991::/var/lib/setroubleshoot:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
[deng@localhost test]$ 

4.27 X\{m\}匹配字符X m次

[deng@localhost test]$ grep 'o\{2\}' /etc/passwd
root:x:0:0:root:/root:/bin/bash
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
setroubleshoot:x:994:991::/var/lib/setroubleshoot:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
[deng@localhost test]$ 

4.28 \+ 匹配前面的字符或者字符串1次或者多次

[deng@localhost test]$ grep 'root\+' /etc/passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin

4.29 ? 匹配前面的字符或者字符串0 次或者 多次

[deng@localhost test]$ grep 'root\?' /etc/passwd
[deng@localhost test]$ grep 'root\?' /etc/passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
[deng@localhost test]$ 

4.30 \用来屏蔽元字符的特殊含义

//匹配.
[deng@localhost test]$ ifconfig  ens33 | grep '\.'
        inet 192.168.1.104  netmask 255.255.255.0  broadcast 192.168.1.255
        RX packets 102516085  bytes 13627338339 (12.6 GiB)
        TX packets 145027  bytes 41135471 (39.2 MiB)
[deng@localhost test]$ 

4.31 匹配 [ ]内的字符

如 [ 1] 即匹配含有字符’1’的字符串

[deng@localhost test]$ grep [1] /etc/passwd

如 [ a] 即匹配含有字符’a’的字符串

[deng@localhost test]$ grep '[a]' /etc/passwd
root:x:0:0:root:/root:/bin/bash
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin

如 [123] 即匹配含有字符’1’ 或者 ’2’ 或者’3’ 的字符串

[deng@localhost test]$ grep '[123]' /etc/passwd
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin

也可以使用字符序列,用字符 ‘-’ 代表字符序

如 [ 1-3 ] 即匹配含有字符’1’ 或者 ’2’ 或者’3’ 的字符串

[deng@localhost test]$ grep '[1-3]' /etc/passwd
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin

如 [ 1-3 a-b] 即匹配含有字符’1’ 或者 ’2’ 或者’3’ 或者 ’a’ 或者 ’b’的字符串

[deng@localhost test]$ grep '[1-3a-b]' /etc/passwd

4.32 .匹配任意的单字符

任意两个字符开头,然后第三个字符为 ‘3’

[deng@localhost test]$ grep '^..3' /etc/passwd

05. 附录

参考:【Linux】一步一步学Linux系列教程汇总

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值