【每天一个Linux命令】16. 强大的文件搜索工具grep

命令用途


grep (global search regular expression(RE) and print out the line,全面搜索正则表达式并把行打印出来)
grep 是一种强大的文本搜索工具,它能使用正则表达式搜索文本,并把匹配的行打印出来,不会修改原文件内容。

 

 

命令语法

grep [OPTIONS] PATTERN [FILE...] grep [OPTIONS] [-e PATTERN | -f FILE] [FILE...]
其中option为grep命令的选项,pattern为要匹配的简单字符串或携带特殊字符的模式字符串,-e可以指定多个要匹配的pattern,file为文件列表,可有多个文件

如果在File参数中指定了多个名称,grep命令将显示包含匹配行的文件的名称。对 shell 有特殊含义的字符 ($, *, [, |, ^, (, ), \ ) 出现在 Pattern参数中时必须带双引号。

 

 

 

 

 

参数列表

 

 

 

grep --help

匹配模式选择:
 -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           a data line ends in 0 byte, not newline


杂项:
 -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       show only the part of a line matching PATTERN
 -q, --quiet, --silent     不显示任何东西
 --binary-files=TYPE   assume that binary files are TYPE
 TYPE is 'binary', 'text', or 'without-match'
 -a, --text                匹配二进制的东西
 -I                        不匹配二进制的东西
 -d, --directories=ACTION  目录操作,读取,递归,跳过
 ACTION is 'read', 'recurse', or 'skip'
 -D, --devices=ACTION      设置对设备,FIFO,管道的操作,读取,跳过
 ACTION is 'read' or 'skip'
 -R, -r, --recursive       递归调用
 --include=PATTERN     files that match PATTERN will be examined
 --exclude=PATTERN     files that match PATTERN will be skipped.
 --exclude-from=FILE   files that match PATTERN in FILE will be skipped.
 -L, --files-without-match 匹配多个文件时,显示不匹配的文件名
 -l, --files-with-matches  查询多文件时只输出包含匹配字符的文件名
 -c, --count               打印匹配的行数
 -Z, --null                print 0 byte after FILE name


文件控制:
 -B, --before-context=NUM  打印匹配本身以及前面的几个行由NUM控制
 -A, --after-context=NUM   打印匹配本身以及随后的几个行由NUM控制
 -C, --context=NUM         打印匹配本身以及随后,前面的几个行由NUM控制
 -NUM                      根-C的用法一样的
 --color[=WHEN],
 --colour[=WHEN]       use markers to distinguish the matching string
 WHEN may be `always', `never' or `auto'.
 -U, --binary              do not strip CR characters at EOL (MSDOS)
 -u, --unix-byte-offsets   report offsets as if CRs were not there (MSDOS)

 

 

命令实例

 

1. 同时显示匹配行上下的?行

 

格式:grep -2 pattern filename同时显示匹配行的上下2行。

 

#例子:显示D/BatteryService( 2732): update start'的上下两行
bixiaopeng@bixiaopengtekiMacBook-Pro ~$ ls -al |grep -2 'D/BatteryService( 2732): update start' xiami.log

 

2. 打印匹配行前面打印该行所在的块号码 -b

 

 

 

bixiaopeng@bixiaopengtekiMacBook-Pro ~$ ls -al |grep -b 'D/BatteryService( 2732): update start' xiami.log 

 

3. 只打印匹配的行数,不显示匹配的内容 -c

 

bixiaopeng@bixiaopengtekiMacBook-Pro ~$ ls -al |grep -c 'D/BatteryService( 2732): update start' xiami.log
21

 

4. 当搜索多个文件时,不显示匹配文件名前缀 -h

 

bixiaopeng@bixiaopengtekiMacBook-Pro ~$ ls -al |grep -h 'D/BatteryService( 2732): update start' xiami.log testgrep
09-03 10:59:48.537 D/BatteryService( 2732): update start
09-03 11:00:28.601 D/BatteryService( 2732): update start

 

5. 取消显示,只返回退出状态。0则表示找到了匹配的行  -q

 

bixiaopeng@bixiaopengtekiMacBook-Pro ~$ ls -al |grep -q 'D/BatteryService( 2732): update start' xiami.log
bixiaopeng@bixiaopengtekiMacBook-Pro ~$ echo $?
0
bixiaopeng@bixiaopengtekiMacBook-Pro ~$ ls -al |grep -q 'bixiaopeng' xiami.log
bixiaopeng@bixiaopengtekiMacBook-Pro ~$ echo $?
1

 

6. 打印匹配模板的文件清单 -l

 

#查看哪个文件中包含字符 bixiaopeng
bixiaopeng@bixiaopengtekiMacBook-Pro ~$ grep -l 'bixiaopeng' xiami.log testgrep
testgrep

 

7. 打印不匹配模板的文件清单 -L

 

#查看哪个文件中不包含字符 bixiaopeng
bixiaopeng@bixiaopengtekiMacBook-Pro ~$ grep -L 'bixiaopeng' xiami.log testgrep
xiami.log

 

8. 在匹配的行前面打印行号 -n

 

#打印出bixiaopeng所在的行号
bixiaopeng@bixiaopengtekiMacBook-Pro ~$ grep -n 'bixiaopeng' testgrep
10:my name is bixiaopeng
11:bixiaopeng is my name

 

9. 不显示关于不存在或者无法读取文件的错误信息 -s

 

#不加-s的情况
bixiaopeng@bixiaopengtekiMacBook-Pro ~$ grep -n 'hahahah' testgreps
grep: testgreps: No such file or directory
#加了-s以后
bixiaopeng@bixiaopengtekiMacBook-Pro ~$ grep -s 'hahahah' testgreps
bixiaopeng@bixiaopengtekiMacBook-Pro ~$

 

10. 反检索,只显示不匹配的行 -v

 

#显示testgrep文件中不包含hahahah的行
bixiaopeng@bixiaopengtekiMacBook-Pro ~$ grep -v 'hahahah' testgrep
wirelessqa is my blog
wirelestqa is testfile

wirelesmqa is testfile
mmmqa is testfile
mmqa
mqa is testfile
Wirelessqa is my blog
my blog is wirelessqa
my name is bixiaopeng
bixiaopeng is my name
bxp is my name
my name is bxp
my name is (bxp)
b$##

 

11. 如果被\<和\>引用,就把表达式做为一个单词搜索 -w

 

bixiaopeng@bixiaopengtekiMacBook-Pro ~$ grep -w '\<bxp\>' testgrep
bxp is my name
my name is bxp
my name is (bxp)

 

12. 显示软件版本信息 -V

 

bixiaopeng@bixiaopengtekiMacBook-Pro ~$ grep -V
grep (BSD grep) 2.5.1-FreeBSD

 

13. 不区分大小写地搜索。默认情况区分大小写 -i

 

#如果不加 -i搜索结果如下,大写的Wirelessqa不会被显示
bixiaopeng@bixiaopengtekiMacBook-Pro ~$ grep -w '\<wirelessqa\>' testgrep
wirelessqa is my blog
my blog is wirelessqa
#如果加上-i 结果如下
bixiaopeng@bixiaopengtekiMacBook-Pro ~$ grep -wi '\<wirelessqa\>' testgrep
wirelessqa is my blog
Wirelessqa is my blog
my blog is wirelessqa

订阅

微信搜索“毕小烦”或者扫描下面的二维码,即可订阅我的文章。

image.png

如果文章对你有帮助,请随手点个赞吧!

(完)

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

毕小烦

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值