linux 命令 - grep: 正则搜索文本grep 搜索文本文件中与指定正则表达式匹配的行
命令格式grep [OPTIONS] PATTERN [FILE...]
命令参数Generic PRogram Information
--help打印帮助信息
-V, --version打印版本信息
Matcher Selection
-E, --extended-regexp使用基本正则表达式(ERE)解释 PATTERN
-F, --fixed-strings每个模式作为一组固定字符串对待(以新行分隔),而不作为正则表达式。
-G, --basic-regexp使用基本正则表达式(BRE)解释 PATTERN
-P, --perl-regexp使用 Perl 正则表达式(PCRE)解释 PATTERN
Matching Control
-e PATTERN, --regexp=PATTERN使用 PATTERN 作为匹配模式
-f FILE, --file=FILE从文件获取匹配模式
-i, --ignore-case忽略大小写
-v, --invert-match输出不含匹配项的行
-w, --Word-regexp单词精确匹配
-x, --line-regexp行精确匹配
General Output Control
-c, --count输出匹配项的数目
-l, --files-with-matches输出包含匹配项的文件名而不是直接输出匹配行
-L, --files-without-match与 -l 选项类似,输出的是包含不匹配项的文件名
-m NUM, --max-count=NUM当匹配 NUM 后停止读取文件
-o, --only-matching只显示匹配项而不是匹配行
-q, --quiet, --silent安静模式,不显示任何信息
-s, --no-messages当文件不存在或不可读时,不显示错误信息
Output Line Prefix Control
-b, --byte-offset在每个匹配行前加上该行在文件内的块号
-H, --with-filename在每个匹配行前加上文件名
-h, --no-filename多文件搜索时,抑制文件名输出
-n, --line-number在每个匹配行前加上该行在文件内的行号
Context Line Control
-A NUM, --after-context=NUM输出匹配行及其后 NUM 行的内容
-B NUM, --before-context=NUM输出匹配行及其前 NUM 行的内容
-C NUM, -NUM, --context=NUM输出匹配行及其前后 NUM 行的内容
实例测试文件 test.txt:
The Zen of Python, by Tim PetersBeautiful is better than uglyExplicit is better than implicitSimple is better than complexComplex is better than complicatedFlat is better than nestedSparse is better than denseReadability countsSpecial cases aren't special enough to break the rulesAlthough practicality beats purityErrors should never pass silentlyUnless explicitly silencedIn the face of ambiguity, refuse the temptation to guessThere should be one-- and preferably only one --obvious way to do itAlthough that way may not be obvious at first unless you're DutchNow is better than neverAlthough never is often better than *right* nowIf the implementation is hard to explain, it's a bad ideaIf the implementation is easy to explain, it may be a good ideaNamespaces are one honking great idea -- let's do more of those
a) 输出含有 "com" 的行
huey@huey-K42JE:~/huey/linux/cmdline$ grep com test.txtSimple is better than complexComplex is better than complicated
b) 匹配时忽略大小写
huey@huey-K42JE:~/huey/linux/cmdline$ grep -i com test.txtSimple is better than complexComplex is better than complicated
c) 输出以 "complex" 开头的行,忽略大小写
huey@huey-K42JE:~/huey/linux/cmdline$ grep -i '^complex' test.txtComplex is better than complicated
d) 输出以 "idea" 结尾的行
huey@huey-K42JE:~/huey/linux/cmdline$ grep 'idea$' test.txtIf the implementation is hard to explain, it's a bad ideaIf the implementation is easy to explain, it may be a good idea
e) 匹配空行
huey@huey-K42JE:~/huey/linux/cmdline$ grep -n '^$' test.txt2:
h) 输出含有 "good" 或 "bad" 的行
huey@huey-K42JE:~/huey/linux/cmdline$ grep 'good\|bad' test.txtIf the implementation is hard to explain, it's a bad ideaIf the implementation is easy to explain, it may be a good ideahuey@huey-K42JE:~/huey/linux/cmdline$ grep -E 'good|bad' test.txtIf the implementation is hard to explain, it's a bad ideaIf the implementation is easy to explain, it may be a good idea
i) 精确匹配单词 it,像 implicit、purity、purity 等这样的单词中的 it 部分是不会被匹配的
huey@huey-K42JE:~/huey/linux/cmdline$ grep -w it test.txtThere should be one-- and preferably only one --obvious way to do itIf the implementation is hard to explain, it's a bad ideaIf the implementation is easy to explain, it may be a good idea
j) 输出含有 "Python" 的行及其后 3 行
huey@huey-K42JE:~/huey/linux/cmdline$ grep -A 3 Python test.txtThe Zen of Python, by Tim PetersBeautiful is better than uglyExplicit is better than implicit
相关命令egrep - 相当于 grep -E
fgrep - 相当于 grep -F
pgrep - 相当于 grep -P