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 Peters

Beautiful is better than ugly
Explicit is better than implicit
Simple is better than complex
Complex is better than complicated
Flat is better than nested
Sparse is better than dense
Readability counts
Special cases aren't special enough to break the rules
Although practicality beats purity
Errors should never pass silently
Unless explicitly silenced
In the face of ambiguity, refuse the temptation to guess
There should be one-- and preferably only one --obvious way to do it
Although that way may not be obvious at first unless you're Dutch
Now is better than never
Although never is often better than *right* now
If the implementation is hard to explain, it's a bad idea
If the implementation is easy to explain, it may be a good idea
Namespaces are one honking great idea -- let's do more of those

a) 输出含有 "com" 的行

huey@huey-K42JE:~/huey/linux/cmdline$ grep com test.txt
Simple is better than complex
Complex is better than complicated

b) 匹配时忽略大小写

huey@huey-K42JE:~/huey/linux/cmdline$ grep -i com test.txt
Simple is better than complex
Complex is better than complicated

c) 输出以 "complex" 开头的行,忽略大小写

huey@huey-K42JE:~/huey/linux/cmdline$ grep -i '^complex' test.txt
Complex is better than complicated

d) 输出以 "idea" 结尾的行

huey@huey-K42JE:~/huey/linux/cmdline$ grep 'idea$' test.txt
If the implementation is hard to explain, it's a bad idea
If the implementation is easy to explain, it may be a good idea

e) 匹配空行

huey@huey-K42JE:~/huey/linux/cmdline$ grep -n '^$' test.txt
:

h) 输出含有 "good" 或 "bad" 的行

huey@huey-K42JE:~/huey/linux/cmdline$ grep 'good\|bad' test.txt
If the implementation is hard to explain, it's a bad idea
If the implementation is easy to explain, it may be a good idea
huey@huey-K42JE:~/huey/linux/cmdline$ grep -E 'good|bad' test.txt
If the implementation is hard to explain, it's a bad idea
If 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.txt
There should be one-- and preferably only one --obvious way to do it
If the implementation is hard to explain, it's a bad idea
If 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.txt
The Zen of Python, by Tim Peters Beautiful is better than ugly
Explicit is better than implicit

相关命令

egrep - 相当于 grep -E

fgrep - 相当于 grep -F

pgrep - 相当于 grep -P

Linux 命令 - grep: 正则搜索文本的更多相关文章

  1. linux 命令grep

    linux 命令grep grep命令用来搜索文本,或从给定的文件中搜索行内包含了给定字符串或单词的文件.通常来说,grep显示匹配的行.使用grep来搜索包括一个或多个正则表达式匹配到的文本行,然后 ...

  2. Linux 命令行下搜索工具大盘点,效率提高不止一倍!

    在 Linux 命令行下进行文本关键字的搜索,大家肯定第一时间会想到 grep 命令.grep 命令确实十分强大,但如果需要用到它更加灵活的功能时,可能命令就会显得十分复杂. 于是,为了简化 grep ...

  3. 【linux】linux命令grep + awk 详解

    linux命令grep  +  awk 详解 grep:https://www.cnblogs.com/flyor/p/6411140.html awk:https://www.cnblogs.com ...

  4. 500 多个 Linux 命令文档搜索

    500 多个 Linux 命令文档搜索 搜索界面:https://wangchujiang.com/linux-command/ 源码:https://github.com/jaywcjlove/li ...

  5. Linux 命令——grep | 正则表达式

    感觉讲的很详细,瞬间懂了grep,正则. from: here 简介 grep (global search regular expression(RE) and print out the line ...

  6. 菜鸟学Linux命令:grep配合ls等使用

    linux grep命令 (global search regular expression(RE) and print out the line )是一种强大的文本搜索工具,它能使用正则表达式搜索文 ...

  7. (转)Linux命令grep

    场景:grep命令在文件搜索中经常会使用到,所以熟练掌握该命令对于日常日志搜索相当有必要! Linux系统中grep命令是一种强大的文本搜索工具,它能使用正则表达式搜索文本,并把匹 配的行打印出来.g ...

  8. [ Linux 命令 ] grep

    一.grep是什么? Linux grep命令是用于查找文件里符合条件行的shell命令. 二.为什么要使用grep? 在查找文件内容时候,通过使用grep指定条件,可以快速定位到文件里字符串所在的行 ...

  9. Linux命令-grep

    grep命令用于对文本进行搜索,格式为“grep [选项] [文件]” 搜索某个关键词:"grep 关键词 文本文件" 参数说明 -b 将可执行文件当做文本文件来搜索 -c 仅显示 ...

随机推荐

  1. UVaLive 6628 Grachten (水题,数学)

    题意:给定一个平面图形并且且给了几条连,求一条. 析:简单么,三角形相似,很简单就AC. 代码如下: #pragma comment(linker, "/STACK:1024000000,1 ...

  2. SCOI2016滚粗记

    day0 又到了SCOI,照惯例赛前参加省选培训,住酒店但学校食堂很难吃. 省选培训被成七和南山的大爷虐翻,感觉进省队没什么戏,权当玩一玩吧. day1 早上醒的时候感觉脑袋很痛,想睡又睡不着,第二天 ...

  3. Java学习笔记(二):String

    String 在Java中String是作为引用对象存在的一种数据类型,用来保存字符串. 实例化和赋值 //直接声明 String s1 = "Hello world!"; //通 ...

  4. Unity3D之ScriptableObject学习笔记

    不同与C#提供的Serializable序列化功能,ScriptableObject是Unity3D提供的一个数据存储类,我们接下来学习一下这个类的功能. 官方文档 http://docs.unity ...

  5. 十六进制转十进制 - C

    我们经常碰到16进制数转10进制的情况,使用下面的C程序即可完成上述工作. 那么他是怎样的工作原理呢? 6.2.5 十六进制数转换成十进制数 16进制就是逢16进1,但我们只有0~9这十个数字,所以我 ...

  6. linux 下httpd服务开机启动

    分类: 网站搭建2011-01-07 05:52 1164人阅读 评论(0) 收藏 举报 linuxapache 我的apache安装目录在 /usr/local/apache 有2种方法可以设置开机 ...

  7. Centos上Apache重启,mysql重启, nginx 重启方法

    1.重启 apache service httpd restrat /etc/init.d/httpd stop /etc/init.d/httpd start 2.重启 mysql service ...

  8. Codeforces GYM 100114 D. Selection 线段树维护DP

    D. Selection Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100114 Descriptio ...

  9. BZOJ 4236: JOIOJI MAP

    4236: JOIOJI Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://www.lydsy.com/JudgeOnline/problem.ph ...

  10. Android学习笔记之百度地图

    步行路线搜索及RouteOverlay 方式与驾车路线搜索类似,只需将mMKSearch.drivingSearch(null, start, null, end)修改为mMKSearch.walki ...