grep 命令是一种强大的文本搜索工具。grep(全称:Global Regular Expression Print)命令用于根据给定的正则表达式搜索文本,并将匹配的行打印出来,能配合多种命令使用,使用上十分灵活。
Unix的grep家族包括grep、egrep和fgrep。egrep和fgrep的命令只跟grep有很小不同。egrep是grep的扩展,支持更多的re元字符, fgrep就是fixed grep或fast grep,它们把所有的字母都看作单词,也就是说,正则表达式中的元字符表示回其自身的字面意义,不再特殊。linux使用GNU版本的grep。它功能更强,可以通过-G、-E、-F命令行选项来使用egrep和fgrep的功能。
语法:grep [option] [pattern] file
grep常用参数
- -a --text :# 不要忽略二进制数据。
- -A <n> --after-context=<n> :除了显示匹配的那一行之外,还显示该行之后n行的内容。
- -b --byte-offset :输出匹配所位于的字符或字节偏移。
- -B<n> --before-context=<n> :除了显示匹配的那一行之外,还显示该行之前n行的内容。
- -c --count :只显示有多少行匹配,而不具体显示匹配的行。。
- -C<n> --context=<n>或-<n> :除了显示匹配的那一行之外,还显示该行之前n行和之后n行的内容。
- -m <n> --max-count=<n> :找到n行结果后停止查找,用来限制匹配行数
- -o :只输出文件中匹配到的部分,而不是整行输出。
- -d<action> --directories=<action> :当指定要查找的是目录而非文件时,必须使用这项参数,否则grep命令将回报信息并停止动作。
- -e<patten> --regexp=<patten> :指定字符串作为查找文件内容的匹配样式,可用于启动多个匹配样式。
- -f<pattern filename> --file=<pattern filename> :指定匹配文件,其内容有一个或多个匹配样式,让grep查找符合匹配条件的文件内容,格式为每一行的匹配样式。
- -E --extended-regexp :使用扩展正则表达式,即egrep命令。
- -F --fixed-regexp :将匹配样式视为固定字符串的列表,即fgrep命令。
- -G --basic-regexp :将匹配样式视为普通的表示法来使用。
- -h --no-filename :在显示匹配的那一行之前,不显示匹配的文件名。
- -H --with-filename :在显示匹配的那一行之前,显示匹配的文件名。
- -i --ignore-case :匹配时忽略字符大小写的差别。
- -y :此参数效果跟“-i”相同。
- -l --file-with-matches :只显示文件内容匹配范本样式的文件名清单。
- -L --files-without-match :只显示文件内容不匹配范本样式的文件名称。
- -n --line-number :在显示匹配行之前,显示出该行在文件中的行号。
- -P --perl-regexp :PATTERN 是一个 Perl 正则表达式
- -q --quiet或--silent :不显示任何信息。
- -R/-r --recursive :此参数的效果和指定“-d recurse”参数相同。
- -s --no-messages :不显示错误信息。
- -v --revert-match :反转查找,显示不匹配的行。
- -V --version :显示版本信息。
- -w --word-regexp :只显示完整单词匹配的行。
- -x --line-regexp :只显示完整行的匹配。
- -Z:指定以0值字节作为终结符文件名(\0)。
-
--color=auto :可以将找到的匹配部分加上颜色的显示
正则表达式常用规则
- ^ :锚定行的开始 如:'^grep'匹配所有以grep开头的行。
- $ :锚定行的结束 如:'grep$' 匹配所有以grep结尾的行。
- . :匹配一个非换行符的字符,如:'gr.p'匹配gr后接一个任意字符,然后是p。
- * :匹配0个或多个先前字符,如:' *grep'匹配所有0个或多个空格后紧跟grep的行。
- .* :一起用代表任意字符。
- + :匹配一个或多个先前字符,如:' +grep'匹配所有一个或多个空格后紧跟grep的行。需 -E 选项
- ?:匹配0个或一个先前字符,如:' +grep'匹配所有0个或一个空格后紧跟grep的行。需 -E 选项
- [] :匹配一个指定范围内的字符,如'[Gg]rep'匹配Grep和grep。需 -E 选项
- [^] :匹配一个不在指定范围内的字符,如:'[^A-FH-Z]rep'匹配不包含A-R和T-Z的一个字母开头,紧跟rep的行。需 -E 选项
- (..) :标记匹配字符,如'(love)',love被标记为1。需 -E 选项
- x{m} :重复字符x,m次,如:'0{5}'匹配包含5个o的行。需 -E 选项
- x{m,} :重复字符x,至少m次,如:'o{5,}'匹配至少有5个o的行。需 -E 选项
- x{m,n} :重复字符x,至少m次,不多于n次,如:'o{5,10}'匹配5--10个o的行。需 -E 选项
- \w :匹配文字和数字字符,也就是[A-Za-z0-9],如:'G\w*p'匹配以G后跟零个或多个文字或数字字符,然后是p。
- \W :\w的反置形式,匹配一个非单词字符,如点号句号等。
- \b :单词锁定符,如: '\bgrep\b'只匹配grep。
- < :锚定单词的开始,如:'<grep'匹配包含以grep开头的单词的行。
- > :锚定单词的结束,如'grep>'匹配包含以grep结尾的单词的行。
grep常见用法
在文件中搜索一个单词,命令会返回一个包含 “match_pattern” 的文本行:
$ grep match_pattern file_name
$ grep "match_pattern" file_name
# 在当前目录搜索带'energywise'行的文件
grep ‘energywise’ *
# 将/etc/passwd,有出现 root 的行取出来
$ grep root /etc/passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
或
$ cat /etc/passwd | grep root
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
在多个文件中查找:
grep "match_pattern" file_1 file_2 file_3 ...
搜索命令行历史记录中 输入过 git 命令的记录:
history | grep git
-v 选项
输出除匹配之外的所有行:
$ grep -v "match_pattern" file_name
# 将/etc/passwd,将没有出现 root 的行取出来
$ grep -v root /etc/passwd
# 将/etc/passwd,将没有出现 root 和nologin的行取出来
$ grep -v root /etc/passwd | grep -v nologin
#打印所有不包含Suan Chin的行。
$ grep -v 'Suan Chin' testfile
northwest NW Charles Main 3.0 .98 3 34
western WE Sharon Gray 5.3 .97 5 23
southwest SW Lewis Dalsass 2.7 .8 2 18
southeast SE Patricia Hemenway 4.0 .7 4 17
eastern EA TB Savage 4.4 .84 5 20
northeast NE AM Main Jr. 5.1 .94 3 13
north NO Margot Weber 4.5 .89 5 9
central CT Ann Stephens 5.7 .94 5 13
-e 选项
启动多个匹配样式:
$ echo this is a text line | grep -e "is" -e "line" -o
is
is
line
# 也可以使用 **-f** 选项来匹配多个样式,在样式文件中逐行写出需要匹配的字符。
$ cat patfile
aaa
bbb
$ echo aaa bbb ccc ddd eee | grep -f patfile -o
-E 选项
-E 选项使用扩展正则表达式,使用扩展grep的主要好处是增加了额外的正则表达式元字符集(|、+、()、?、{n,m}):
$ grep -E "[1-9]+"
# 或
$ egrep "[1-9]+"
# 打印所有包含NW或EA的行。如果不是使用egrep,而是grep,将不会有结果查出
$ egrep 'NW|EA' testfile
northwest NW Charles Main 3.0 .98 3 34
eastern EA TB Savage 4.4 .84 5 20
对于标准grep,如果在扩展元字符前面加\,grep会自动启用扩展选项-E。
$ grep 'NW\|EA' testfile
northwest NW Charles Main 3.0 .98 3 34
eastern EA TB Savage 4.4 .84 5 20
搜索所有包含一个或多个3的行:
$ egrep '3+' testfile
# 或
$ grep -E '3+' testfile
# 或
$ grep '3\+' testfile
# 这3条命令将会
northwest NW Charles Main 3.0 .98 3 34
western WE Sharon Gray 5.3 .97 5 23
northeast NE AM Main Jr. 5.1 .94 3 13
central CT Ann Stephens 5.7 .94 5 13
搜索所有包含0个或1个小数点字符的行:
$ egrep '2\.?[0-9]' testfile
$ grep -E '2\.?[0-9]' testfile
$ grep '2\.\?[0-9]' testfile
#首先含有2字符,其后紧跟着0个或1个点,后面再是0和9之间的数字。
western WE Sharon Gray 5.3 .97 5 23
southwest SW Lewis Dalsass 2.7 .8 2 18
eastern EA TB Savage 4.4 .84 5 20
搜索一个或者多个连续的no的行:
$ egrep '(no)+' testfile
$ grep -E '(no)+' testfile
$ grep '\(no\)\+' testfile
#3个命令返回相同结果,
northwest NW Charles Main 3.0 .98 3 34
northeast NE AM Main Jr. 5.1 .94 3 13
north NO Margot Weber 4.5 .89 5 9
其他例子:
$ grep -E '\w+\W+[ABC]' testfile #首先是一个或者多个字母,紧跟着一个或者多个非字母数字,最后一个是ABC中的一个。
northwest NW Charles Main 3.0 .98 3 34
southern SO Suan Chin 5.1 .95 4 15
northeast NE AM Main Jr. 5.1 .94 3 13
central CT Ann Stephens 5.7 .94 5 13
$ egrep '[Ss](h|u)' testfile
$ grep -E '[Ss](h|u)' testfile
$ grep '[Ss]\(h\|u\)' testfile
#3个命令返回相同结果,即以S或s开头,紧跟着h或者u的行。
western WE Sharon Gray 5.3 .97 5 23
southern SO Suan Chin 5.1 .95 4 15
$ egrep 'w(es)t.*\1' testfile
# west开头,其中es为\1的值,后面紧跟着任意数量的任意字符,最后还有一个es出现在该行。
northwest NW Charles Main 3.0 .98 3 34
-f 选项
fgrep 查询速度比grep命令快,但是不够灵活:它只能找固定的文本,而不是规则表达式。
如果你想在一个文件或者输出中找到包含星号字符的行:
$ fgrep '*' /etc/profile
for i in /etc/profile.d/*.sh ; do
# 或
$ grep -F '*' /etc/profile
for i in /etc/profile.d/*.sh ; do
-P 选项
使用Perl正则表达式:
$ grep -P "(\d{3}-){2}\d{4}" file_name
-o 选项
只输出文件中匹配到的部分:
$ echo this is a test line. | grep -o -E "[a-z]+."
line.
$ echo this is a test line. | egrep -o "[a-z]+."
line.
-c 选项
统计文件或者文本中包含匹配字符串的行数:
$ grep -c "text" file_name
#-c使得grep只打印有多少匹配模板的行。
$ grep -c 'west' testfile
3
-C 选项
显示匹配行及该行之前和之后n行:
# 打印匹配行及其上下各两行。
$ grep -C 2 Patricia testfile
southwest SW Lewis Dalsass 2.7 .8 2 18
southern SO Suan Chin 5.1 .95 4 15
southeast SE Patricia Hemenway 4.0 .7 4 17
eastern EA TB Savage 4.4 .84 5 20
northeast NE AM Main Jr. 5.1 .94 3 13
-B 选项
显示匹配行及该行之前n行:
# 打印匹配行及其前两行。
$ grep -B 2 Patricia testfile
southwest SW Lewis Dalsass 2.7 .8 2 18
southern SO Suan Chin 5.1 .95 4 15
southeast SE Patricia Hemenway 4.0 .7 4 17
-A 选项
显示匹配行及该行之后n行:
# 打印匹配行及其后两行。
$ grep -A 2 Patricia testfile
southeast SE Patricia Hemenway 4.0 .7 4 17
eastern EA TB Savage 4.4 .84 5 20
northeast NE AM Main Jr. 5.1 .94 3 13
打印出匹配文本之前或者之后的行:
# 显示匹配某个结果之后的3行,使用 -A 选项:
$ seq 10 | grep "5" -A 3
5
6
7
8
# 显示匹配某个结果之前的3行,使用 -B 选项:
$ seq 10 | grep "5" -B 3
2
3
4
5
# 显示匹配某个结果的前三行和后三行,使用 -C 选项:
$ seq 10 | grep "5" -C 3
2
3
4
5
6
7
8
# 如果匹配结果有多个,会用“--”作为各匹配结果之间的分隔符:
$ echo -e "a\nb\nc\na\nb\nc" | grep a -A 1
a
b
--
a
b
-n 选项
输出包含匹配字符串的行数:
$ grep "text" -n file_name
# 或
$ cat file_name | grep "text" -n
# 多个文件
$ grep "text" -n file_1 file_2
# 将/etc/passwd,有出现 root 的行取出来,同时显示这些行在/etc/passwd的行号
$ grep -n root /etc/passwd
1:root:x:0:0:root:/root:/bin/bash
30:operator:x:11:0:operator:/root:/sbin/nologin
$ grep -n '^south' testfile #-n选项在每一个匹配行的前面打印行号。
3:southwest SW Lewis Dalsass 2.7 .8 2 18
4:southern SO Suan Chin 5.1 .95 4 15
5:southeast SE Patricia Hemenway 4.0 .7 4 17
-b 选项
打印样式匹配所位于的字符或字节偏移:
$ echo gun is not unix | grep -b -o "not"
7:not
# 一行中字符串的字符偏移是从该行的第一个字符开始计算,起始值为0。
# 选项 **-b -o** 一般总是配合使用。
-l 选项
搜索多个文件并查找匹配文本在哪些文件中:
$ grep -l "text" file1 file2 file3...
# -l使得grep只打印匹配的文件名,而不打印匹配的行。
$ grep -l 'ss' testfile
testfile
-r 选项
在多级目录中对文本进行递归搜索:
$ grep "text" . -r -n
# .表示当前目录。
# 在当前目录及其子目录下搜索'energywise'行的文件
$ grep -r ‘energywise’ *
# 在当前目录及其子目录下搜索'energywise'行的文件,但是不显示匹配的行,只显示匹配的文件
$ grep -l -r ‘energywise’ *
-i 选项
忽略匹配样式中的字符大小写:
$ echo "hello world" | grep -i "HELLO"
# hello
$ grep -i 'pat' testfile #-i选项关闭了大小写敏感。
southeast SE Patricia Hemenway 4.0 .7 4 17
-w 选项
只显示整个单词匹配的行:
# -w只打印整个单词匹配的行。
$ grep -w 'north' testfile
north NO Margot Weber 4.5 .89 5 9
--include、--exclude、--exclude-from
在grep搜索结果中包括或者排除指定文件:
# 只在目录中所有的.php和.html文件中递归搜索字符"main()"
$ grep "main()" . -r --include *.{php,html}
# 在搜索结果中排除所有README文件
$ grep "main()" . -r --exclude "README"
# 在搜索结果中排除filelist文件列表里的文件
$ grep "main()" . -r --exclude-from filelist
--color=auto
匹配关键字部分使用颜色显示:
如果每次使用 grep 都得要自行加上 --color=auto ,会显得很麻烦~ 此时可以用 alias 来处理一下。你可以在 ~/.bashrc 内加上这行:『alias grep='grep --color=auto'』再以『 source ~/.bashrc 』来立即生效即可, 这样每次运行 grep 就会自动帮加上颜色显示。
$ grep "match_pattern" file_name --color=auto
# 用 dmesg 列出核心信息,再以 grep 找出内含 eth 那行,要将捉到的关键字显色,且加上行号来表示:
$ dmesg | grep -n --color=auto 'eth'
247:eth0: RealTek RTL8139 at 0xee846000, 00:90:cc:a6:34:84, IRQ 10
248:eth0: Identified 8139 chip type 'RTL-8139C'
294:eth0: link up, 100Mbps, full-duplex, lpa 0xC5E1
305:eth0: no IPv6 routers present
# 你会发现除了 eth 会有特殊颜色来表示之外,最前面还有行号
-Z 选项
使用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结合使用。
-q 选项
grep静默输出:
$ grep -q "test" filename
# 不会输出任何信息,如果命令运行成功返回0,失败则返回非0值。一般用于条件测试。
grep退出状态
- 0: 表示成功;
- 1: 表示在所提供的文件无法找到匹配的pattern;
- 2: 表示参数中提供的文件不存在。
$ grep 'root' /etc/passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
$ echo $?
0
$ grep 'root1' /etc/passwd #用户root1并不存在
$ echo $?
1
$ grep 'root' /etc/passwd1 #这里的/etc/passwd1文件并不存在
grep: /etc/passwd1: No such file or directory
$ echo $?
2
grep正则表达式实例
$ cat testfile
northwest NW Charles Main 3.0 .98 3 34
western WE Sharon Gray 5.3 .97 5 23
southwest SW Lewis Dalsass 5.7 .8 2 18
southern SO Suan Chin 5.1 .95 4 15
southeast SE Patricia Hemenway 4.0 .7 4 17
eastern EA TB Savage 4.4 .84 5 20
northeast NE AM Main Jr. 5.1 .94 3 13
north NO Margot Weber 4.5 .89 5 9
central CT Ann Stephens 5.7 .94 5 13
$ grep NW testfile #打印出testfile中所有包含NW的行。
northwest NW Charles Main 3.0 .98 3 34
$ grep '^n' testfile #打印出以n开头的行。
northwest NW Charles Main 3.0 .98 3 34
northeast NE AM Main Jr. 5.1 .94 3 13
north NO Margot Weber 4.5 .89 5 9
$ grep '4$' testfile #打印出以4结尾的行。
northwest NW Charles Main 3.0 .98 3 34
$ grep '5\..' testfile #打印出第一个字符是5,后面跟着一个.字符,在后面是任意字符的行。
western WE Sharon Gray 5.3 .97 5 23
southern SO Suan Chin 5.1 .95 4 15
northeast NE AM Main Jr. 5.1 .94 3 13
central CT Ann Stephens 5.7 .94 5 13
$ grep '\.5' testfile #打印出所有包含.5的行。
north NO Margot Weber 4.5 .89 5 9
$ grep '^[we]' testfile #打印出所有以w或e开头的行。
western WE Sharon Gray 5.3 .97 5 23
eastern EA TB Savage 4.4 .84 5 20
$ grep '[^0-9]' testfile #打印出所有不是以0-9开头的行。
northwest NW Charles Main 3.0 .98 3 34
western WE Sharon Gray 5.3 .97 5 23
southwest SW Lewis Dalsass 2.7 .8 2 18
southern SO Suan Chin 5.1 .95 4 15
southeast SE Patricia Hemenway 4.0 .7 4 17
eastern EA TB Savage 4.4 .84 5 20
northeast NE AM Main Jr. 5.1 .94 3 13
north NO Margot Weber 4.5 .89 5 9
central CT Ann Stephens 5.7 .94 5 13
$ grep '[A-Z][A-Z] [A-Z]' testfile #打印出所有包含前两个字符是大写字符,后面紧跟一个空格及一个大写字母的行。
eastern EA TB Savage 4.4 .84 5 20
northeast NE AM Main Jr. 5.1 .94 3 13
#注:在执行以上命令时,如果不能得到预期的结果,即grep忽略了大小写,导致这一问题的原因很可能是当前环境的本地化的设置问题。对于以上命令,如果我将当前语言设置为en_US的时候,它会打印出所有的行,当我将其修改为中文环境时,就能得到我现在的输出了。
$ export LANG=zh_CN #设置当前的语言环境为中文。
$ export LANG=en_US #设置当前的语言环境为美国。
$ export LANG=en_Br #设置当前的语言环境为英国。
$ grep '[a-z]\{9\}' testfile #打印所有包含每个字符串至少有9个连续小写字符的字符串的行。
northwest NW Charles Main 3.0 .98 3 34
southwest SW Lewis Dalsass 2.7 .8 2 18
southeast SE Patricia Hemenway 4.0 .7 4 17
northeast NE AM Main Jr. 5.1 .94 3 13
# 第一个字符是3,紧跟着一个句点,然后是任意一个数字,然后是任意个任意字符,然后又是一个3,然后是制表符,然后又是一个3,需要说明的是,下面正则中的\1表示\(3\)。
$ grep '\(3\)\.[0-9].*\1 *\1' testfile
northwest NW Charles Main 3.0 .98 3 34
$ grep '\<north' testfile #打印所有以north开头的单词的行。
northwest NW Charles Main 3.0 .98 3 34
northeast NE AM Main Jr. 5.1 .94 3 13
north NO Margot Weber 4.5 .89 5 9
$ grep '\<north\>' testfile #打印所有包含单词north的行。
north NO Margot Weber 4.5 .89 5 9
$ grep '^n\w*' testfile #第一个字符是n,后面是任意字母或者数字。
northwest NW Charles Main 3.0 .98 3 34
northeast NE AM Main Jr. 5.1 .94 3 13
north NO Margot Weber 4.5 .89 5 9
参考
https://www.cnblogs.com/wordless/p/16208858.html
https://www.cnblogs.com/orangeform/archive/2011/11/14/2243694.html