文章目录

  • grep
  • 语法
  • 选项
  • 常见用法
  • 递归搜索文件


grep

grep差不多可以说是我们日常中最常用的命令之一,它的全称是“global search regular expression(RE) and print out the line,全面搜索正则表达式并把行打印出来”,是一种强大的文本搜索工具,它能使用正则表达式搜索文本,并把匹配的行打印出来。

语法

grep (选项)(查找的文本内容)(文件)

选项

-a 不要忽略二进制数据。
-A<显示列数> 除了显示符合范本样式的那一行之外,并显示该行之后的内容。
-b 在显示符合范本样式的那一行之外,并显示该行之前的内容。
-c 计算符合范本样式的列数。
-C<显示列数>或-<显示列数> 除了显示符合范本样式的那一列之外,并显示该列之前后的内容。
-d<进行动作> 当指定要查找的是目录而非文件时,必须使用这项参数,否则grep命令将回报信息并停止动作。
-e<范本样式> 指定字符串作为查找文件内容的范本样式。
-E 将范本样式为延伸的普通表示法来使用,意味着使用能使用扩展正则表达式。
-f<范本文件> 指定范本文件,其内容有一个或多个范本样式,让grep查找符合范本条件的文件内容,格式为每一列的范本样式。
-F 将范本样式视为固定字符串的列表。
-G 将范本样式视为普通的表示法来使用。
-h 在显示符合范本样式的那一列之前,不标示该列所属的文件名称。
-H 在显示符合范本样式的那一列之前,标示该列的文件名称。
-i 忽略字符大小写的差别。
-l 列出文件内容符合指定的范本样式的文件名称。
-L 列出文件内容不符合指定的范本样式的文件名称。
-n 在显示符合范本样式的那一列之前,标示出该列的编号。
-q 不显示任何信息。
-R/-r 此参数的效果和指定“-d recurse”参数相同。
-s 不显示错误信息。
-v 反转查找。
-w 只显示全字符合的列。
-x 只显示全列符合的列。
-y 此参数效果跟“-i”相同。
-o 只输出文件中匹配到的部分。

常见用法

在文件中搜索一个单词,命令会返回一个包含**“test”**的文本行:

[root@VM_0_2_centos grep]# cat log.txt 
this is a test.

this is a test1.

this is a test2.

this is a test3.

[root@VM_0_2_centos grep]# grep test log.txt 
this is a test.
this is a test1.
this is a test2.
this is a test3.

在多个文件中查找:

[root@VM_0_2_centos grep]# grep "test" log.txt log1.txt 
log.txt:this is a test.
log.txt:this is a test1.
log.txt:this is a test2.
log.txt:this is a test3.
log1.txt:this is a test.
log1.txt:this is a test1.
log1.txt:this is a test2.
log1.txt:this is a test3.

输出除之外的所有行 -v 选项:

[root@VM_0_2_centos grep]# grep -v "this is a test3" log.txt 
this is a test.

this is a test1.

this is a test2.

标记匹配颜色 –color=auto 选项:

[root@VM_0_2_centos grep]# grep "this is a test3" log.txt --color=auto
this is a test3.

使用正则表达式 -E 选项:

[root@VM_0_2_centos grep]# cat number.txt 
1
2
3
4
5
6
7
8
9
10
11
12

[root@VM_0_2_centos grep]# grep -E "[1-5]+" number.txt 
1
2
3
4
5
10
11
12

只输出文件中匹配到的部分 -o 选项:

[root@VM_0_2_centos grep]# echo this is a test line. | grep -o -E "[a-z]+\."
line.

统计文件或者文本中包含匹配字符串的行数 -c 选项:

[root@VM_0_2_centos grep]# grep -c "test" log.txt 
4

输出包含匹配字符串的行数 -n 选项:

[root@VM_0_2_centos grep]# grep -n "test" log.txt 
1:this is a test.
3:this is a test1.
5:this is a test2.
7:this is a test3.

搜索多个文件并查找匹配文本在哪些文件中:

[root@VM_0_2_centos grep]# grep -l test3 log.txt log1.txt number.txt 
log.txt
log1.txt

递归搜索文件

在多级目录中对文本进行递归搜索:

[root@VM_0_2_centos grep]# grep "test" . -r -n
./log.txt:1:this is a test.
./log.txt:3:this is a test1.
./log.txt:5:this is a test2.
./log.txt:7:this is a test3.
./log1.txt:1:this is a test.
./log1.txt:3:this is a test1.
./log1.txt:5:this is a test2.
./log1.txt:7:this is a test3.

# .表示当前目录。

忽略匹配样式中的字符大小写:

[root@VM_0_2_centos grep]# grep -i "TEST" log.txt 
this is a test.
this is a test1.
this is a test2.
this is a test3.

选项 -e 制动多个匹配样式:

[root@VM_0_2_centos grep]# grep -e "test3" -e "test2" log.txt 
this is a test2.
this is a test3.

也可以使用 -f 选项来匹配多个样式,后面的参数为样式文件,在样式文件中逐行写出需要匹配的字符。

[root@VM_0_2_centos grep]# cat log.txt 
this is a test.

this is a test1.

this is a test2.

this is a test3.
[root@VM_0_2_centos grep]# cat log1.txt 
this is a test.

this is a test1.

this is a test2.

this is a test3.
[root@VM_0_2_centos grep]# grep -f log1.txt log.txt 
this is a test.

this is a test1.

this is a test2.

this is a test3.

在grep搜索结果中包括或者排除指定文件:

#只在目录中所有的.html文件中递归搜索字符"test"
[root@VM_0_2_centos grep]# grep "test" . -r --include *.html
./log.html:this is a test.
./log.html:this is a test1.
./log.html:this is a test2.
./log.html:this is a test3.

#只在目录中所有的.php和.html文件中递归搜索字符"test"
[root@VM_0_2_centos grep]# grep "test" . -r --include *.{php,html}
log.html:this is a test.
log.html:this is a test1.
log.html:this is a test2.
log.html:this is a test3.

#在搜索结果中排除所有html文件
[root@VM_0_2_centos grep]# grep "test" . -r --exclude *.html      
./log.txt:this is a test.
./log.txt:this is a test1.
./log.txt:this is a test2.
./log.txt:this is a test3.
./log1.txt:this is a test.
./log1.txt:this is a test1.
./log1.txt:this is a test2.
./log1.txt:this is a test3.

#在搜索结果中排除filelist文件列表里的文件
[root@VM_0_2_centos grep]# cat filelist 
log.html

[root@VM_0_2_centos grep]# grep "test" . -r --exclude-from filelist 
./log.txt:this is a test.
./log.txt:this is a test1.
./log.txt:this is a test2.
./log.txt:this is a test3.
./log1.txt:this is a test.
./log1.txt:this is a test1.
./log1.txt:this is a test2.
./log1.txt:this is a test3.

打印出匹配文本之前或者之后的行:

#显示匹配某个结果之后的3行,使用 -A 选项:
[root@VM_0_2_centos grep]# grep "3" -A 3 number.txt 
3
4
5
6

#显示匹配某个结果之前的3行,使用 -B 选项:
[root@VM_0_2_centos grep]# grep "4" -B 3 number.txt 
1
2
3
4

#显示匹配某个结果的前三行和后三行,使用 -C 选项:
[root@VM_0_2_centos grep]# grep "4" -C 3 number.txt 
1
2
3
4