grep [OPTIONS] PATTERN [FILE...]
grep [OPTIONS] [-e PATTERN | -f FILE] [FILE...]
功能
grep 是一种强大的文本搜索工具,它能使用正则表达式搜索(指定文件中的)文本,并把匹配的行打印出来。
举例
显示测试文件 testFile 的内容
$cat testFile
输入之后,输出如下:
hello, this is a file
this file used for test the
command of grep.
do you know?
yes yeeeeeees i knooooooww!
the west is
what is ves?
this is a world
which is tes good.
west tes
westeses
westeseses
west tesves
这就是我们要测试的文件的内容。
在当前目录及子目录所有文件中查找字符串 test
$grep -r 'test' *
输入之后,输出如下:
good/test2:this is a test for the
good/test2:test the string.
test2:grep test for
test2:used to test the
test3:grep test for
test3:used to test the
testFile:this file used for test the
这个应该是最常使用的命令了。 -r 选项的意思就是递归搜索子目录的意思。
查找 test 并给结果中匹配的字符串加上颜色
$grep --color 'test' *
这样,会把输出结果中的所有匹配的 test 字符串标上颜色。
要在当前目录下所有 .doc 文件中查找字符串 sort
$grep "sort" *.doc
显示匹配得行数
$grep -c "we" testFile
输入之后,输出如下:
5
这样,显示匹配 we 的行的数目。
显示含有 we 字符的行,并打印行号
$grep -n "we" testFile
输入之后,输出如下:
6:the west is
10:west tes
11:westeses
12:westeseses
13:west tesves
这样,会在输出的结果的最左侧打印匹配的行号。
显示不匹配 we 的行
$grep -v "we" testFile
输入之后,输出如下:
hello, this is a file
this file used for test the
command of grep.
do you know?
yes yeeeeeees i knooooooww!
what is ves?
this is a world
which is tes good.
这里 -v 选项表示显示不匹配 we 的行。
忽略大小写的匹配
$ls |grep -i 'file'
输入之后,输出如下:
testFile
这样,添加了 -i 选项,匹配 file 的时候,不会区分大小写。
范围的匹配
$grep 'h[ae]' testFile
输入之后,输出如下:
hello, this is a file
this file used for test the
the west is
what is ves?
这里匹配的或者是 ha 或者是 he ,若使用 [a,e] 这样也包括 ‘,’ 逗号了。
仅显示包含 test 匹配的文件
$grep -l 'test' *
输入之后,输出如下:
test2
test3
testFile
这里, -l 选项仅列出含有匹配字符串的文件。
显示 ls -l 输出内容中,以 d 开头的行
$ls -l | grep '^d'
或
$ls -l |grep ^d
输入之后,输出如下:
drwxr-xr-x 2 vaqeteart member 4096 Jul 12 18:44 good
这里, good 是一个目录。
显示 ls -l 输出内容中,不以 d 开头的行
$ls -l |grep '^[^d]'
输入之后,输出如下:
total 16
-rw-r--r-- 1 vaqeteart member 7 Jul 12 18:42 2
-rw-r--r-- 1 vaqeteart member 114 Jul 12 18:42 test2
-rw-r--r-- 1 vaqeteart member 212 Jul 12 19:01 testFile
这里,利用 [^] 来指定不包含的字符。
显示所有以 t 开头的文件中包含 test 的行
$grep 'test' t*
输入之后,输出如下:
test2:grep test for
test2:used to test the
testFile:this file used for test the
这里,冒号左侧是对应行的文件名。
显示在 testFile , test2 文件中匹配 test 的行
$grep 'test' testFile test2
或
$grep test testFile test2
输入之后,输出如下:
testFile:this file used for test the
test2:grep test for
test2:used to test the
为保险期间,后面最好在搜索的时候加上''.
显示 testFile 中有至少5个连续小写字符的字符串的行
$grep '[a-z]\{5\}' testFile
输入之后,输出如下:
hello, this is a file
command of grep.
yes yeeeeeees i knooooooww!
this is a world
which is tes good.
westeses
westeseses
west tesves
显示 testFile 中有开头为5个连续小写字符的字符串的行
$grep '^[a-z]\{5,\}' testFile
输入之后,输出如下:
hello, this is a file
command of grep.
which is tes good.
westeses
westeseses
显示 testFile 中含有以 th 为开始的单词的行
$grep '\<th'
输入之后,输出如下:
hello, this is a file
this file used for test the
the west is
this is a world
这里,以 th 为开始的单词有: this 和 the 。
显示 testFile 中含有以 st 结尾的单词的行
$grep 'st\>' testFile
输入之后,输出如下:
this file used for test the
the west is
west tes
west tesves
这里,以 st 结尾的单词有 test 和 west 。
利用标记的搜索
$ grep 'w\(es\)t t\1v\1' testFile
输入之后,输出如下:
west tesves
这里,根据正则表达式 \(\ 括号内的内容 es 匹配之后会被标记为1,这样以后可以用 \1 来代替 es 了。这里相当于搜索: grep 'west tesves' testFile 。如果用 egrep 或 grep -E ,就不用 \ 号进行转义可以直接用1这个标号了。
描述
grep 是一种强大的文本搜索工具,它能使用正则表达式搜索文本,并把匹配的行打印出来。
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 的工作方式是这样的,它在一个或多个文件中搜索字符串模板。如果模板包括空格,则必须被引用,模板后的所有字符串被看作文件名。搜索的结果被送到屏幕,不影响原文件内容。
grep 可用于 shell 脚本,因为 grep 通过返回一个状态值来说明搜索的状态,如果模板搜索成功,则返回 0 ,如果搜索不成功,则返回 1 ,如果文件不存在,则返回 2 。我们利用这些返回值就可进行一些自动化的文本处理工作。
grep 的一些选项:
-
-?: 同时显示匹配行上下的?行,如:grep -2 pattern filename同时显示匹配行的上下2行。 -
-b,--byte-offset: 打印匹配行前面打印该行所在的块号码。 -
-c,--count: 只打印匹配的行数,不显示匹配的内容。 -
-f File,--file=File: 从文件中提取模板。空文件中包含0个模板,所以什么都不匹配。 -
-h,--no-filename: 当搜索多个文件时,不显示匹配文件名前缀。 -
-i,--ignore-case: 忽略大小写差别。 -
-q,--quiet: 取消显示,只返回退出状态。0则表示找到了匹配的行。 -
-l,--files-with-matches: 打印匹配模板的文件清单。 -
-L,--files-without-match: 打印不匹配模板的文件清单。 -
-n,--line-number: 在匹配的行前面打印行号。 -
-s,--silent: 不显示关于不存在或者无法读取文件的错误信息。 -
-v,--revert-match: 反检索,只显示不匹配的行。 -
-w,--word-regexp: 如果被\<和\>引用,就把表达式做为一个单词搜索。 -
-V,--version: 显示软件版本信息。
其它
用于 grep 正则表达式的元字符集(基本集)
-
^: 锚定行的开始 如:'^grep'匹配所有以grep开头的行。 -
$: 锚定行的结束 如:'grep$'匹配所有以grep结尾的行。 -
.: 匹配一个非换行符的字符 如:'gr.p'匹配gr后接一个任意字符,然后是p。 -
*: 匹配零个或多个先前字符 如:'a*'匹配一个或多个a字符,'*grep'匹配一个或多个空格后紧跟grep。 -
.*: 代表任意字符。 -
[]: 匹配一个指定范围内的字符,如'[Gg]rep'匹配Grep和grep。 -
[^]: 匹配一个不在指定范围内的字符,如:'[^A-FH-Z]rep'匹配不包含A-R和H-Z范围的一个字母开头,紧跟rep的行。 -
\(..\): 标记匹配字符,如'(love)',love被标记为1,这样如果同一表达式再引用love时,只需要利用\1即可。 -
\<: 锚定单词的开始,如:'\>' -
\>:: 锚定单词的结束,如'grep\>'匹配包含以grep结尾的单词的行。 -
x\{m\}: 重复字符x,m次,如:'o\{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: 单词锁定符,如: '\bgrepb\'只匹配grep。
用于 egrep 和 grep -E 的元字符扩展集
-
+: 匹配一个或多个先前的字符。如:'[a-z]+able',匹配一个或多个小写字母后跟able的串,如loveable,enable,disable等。 -
?: 匹配零个或一个先前的字符。如:'gr?p'匹配g后跟一个或没有r字符,然后是p的行。 -
a|b|c: 匹配a或b或c。如:grep|sed匹配grep或sed -
(): 分组符号,如:love(able|rs)ov+匹配loveable或lovers,匹配一个或多个ov。 -
x{m},x{m,},x{m,n}: 作用同x\{m\},x\{m,\},x\{m,n\}
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)



京公网安备 11010502036488号