grep使用
grep使用
参数 | 说明 |
---|---|
-i | 忽略大小写 |
-r | 递归查找目录中的文件进行文本匹配 |
-l | 只显示匹配文件所在的文件名 |
-n | 显示匹配文件所在文件的行号 |
-A NUM | 显示匹配文本后NUM行 |
-B NUM | 显示匹配文本前NUM行 |
-C NUM | 显示匹配文本前后NUM行 |
-v | 反转匹配,匹配不在指定文本的行 |
-o | 只显示匹配的文本 |
-c | 统计匹配文本的行数 |
--color | 高亮显示匹配文本 |
常用实例
grep的基本语法:
grep [OPTIONS] PATTERN [FILE...]
grep [OPTIONS] [-e PATTERN | -f FILE] [FILE...]
以demo.txt为例
shell>cat demo.txt
THIS IS THE 1ST LINE
this is the 2nd line
This is the 3rd line
Two lines above this line is empty.
And this is the last line.
1. 在单文件中查找文本
这是最基本,也是最常用的用法,如查找this出现的行:
shell>grep 'this' demo.txt
this is the 2nd line
Two lines above this line is empty.
And this is the last line.
2. 忽略大小写匹配文本
使用-i参数,忽略(ignore)大小写。
shell>grep -i 'this' demo.txt
THIS IS THE 1ST LINE
this is the 2nd line
This is the 3rd line
Two lines above this line is empty.
And this is the last line.
3.在多文件中匹配文本
比如,我们经常会在工程中搜索某个类、函数或结构的定义,这里以beego工程目录为例,我们要搜索Controller的定义:
shell>grep 'type Controller struct' *.go
controller.go:type Controller struct {
在controller.go文件中找到了Controller的定义。
4.递归目录匹配文本
上面只是在当前目录下搜索多个文件,同样的beego为例,我们想搜索Context在工程中的哪个定义中定义的, 使用-r参数,递归查找目录中的文件:
shell>grep -r 'type Context struct' ./
./context/context.go:type Context struct {
有时候匹配文件可能非常多,如果只想匹配的文本在在哪些文件中,可以通过指定-l实现,如:
shell>grep -r -l 'type Context struct' ./
./context/context.go
5.显示匹配文本所在的行号
如果我们想知道上述例子中Context在context/context.go文件中的哪一行中定义的,可以通过-n参数指定:
shell>grep -r -n 'type Context struct' ./
./context/context.go:59:type Context struct {
6.显示匹配行上下文信息
仍然以查找Context为例,想查看匹配行后(After)5行的信息,可以通过指定-A参数实现
shell>grep -r -n -A 5 'type Context struct' ./
./context/context.go:59:type Context struct {
./context/context.go-60- Input *BeegoInput
./context/context.go-61- Output *BeegoOutput
./context/context.go-62- Request *http.Request
./context/context.go-63- ResponseWriter *Response
./context/context.go-64- _xsrfToken string
同样,想看前(Before)5行的信息,可以通过指定-B参数实现,如果想看匹配行前后5行的上下文(Context)信息,可以通过指定-C参数实现。
7.在文件或标准输入中查找不匹配的行
grep除了可以在文件中查找指定字符串或正则表达式外,还可以接受标准输入,有时候可能想查找不匹配的行,可以通过-v参数指定。比如我们经常会通过ps查找某个进程是否存在,比如看nginx进程:
shell> ps -ef | grep nginx
dev 19638 18198 0 18:18 pts/1 00:00:00 grep nginx
dev 23770 1 0 Apr27 ? 00:00:00 nginx: master process nginx -p /home/dev/repos/itest/nginx/work/ -c conf/nginx.conf
dev 23771 23770 0 Apr27 ? 00:00:00 nginx: worker process
除了查找nginx进程外,还有一个grep进程在,这个时候我们可以通过管道再次用grep -v 'grep'把grep进程过滤掉:
shell>ps -ef | grep nginx | grep -v 'grep'
dev 23770 1 0 Apr27 ? 00:00:00 nginx: master process nginx -p /home/dev/repos/itest/nginx/work/ -c conf/nginx.conf
dev 23771 23770 0 Apr27 ? 00:00:00 nginx: worker process
8.统计匹配行次数
可以通过指定-c参数实现,比如仍以demo.txt为例,想统计this出现的行次数:
shell>grep -c 'this' demo.txt
3
9.统计匹配文本的次数
仍然是demo.txt为例,我们统计单词this出现的次数,忽略大小写,首先需要指定-o参数查找匹配文本,再使用wc -l进行次数统计:
shell>grep -o -i 'this' demo.txt | wc -l
5
10.高亮显示匹配的文本
可以通过指定--color参数实现:
shell>grep -i 'this' --color demo.txt
11.查看某段时间的日志
grep
grep -E '2020-07-21 1[1-2]:[5-0][8-5]:[0-5]0' out.log
sed
sed -n '/2020-07-21 11:58:00/,/2020-07-21 12:05:50/p' out.log
sed -n
-n选项:只显示匹配处理的行(不加会输出所有)
-p选项:打印
-n 和-p 经常一起使用
12.grep 同时满足多个关键字和满足任意关键字
grep -E "word1|word2|word3" file.txt
满足任意条件(word1、word2和word3之一)将匹配。
grep word1 file.txt | grep word2 |grep word3
必须同时满足三个条件(word1、word2和word3)才匹配。