home

Love life, love sports, love to learn, & love...

Blog

Thought,  done  &  record.

订阅到QQ邮箱 google reader google reader

Linux 中 Grep 命令使用方法

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

grep的工作方式是这样的,它在一个或多个文件中搜索字符串模板。如果模板包括空格,则必须被引用,模板后的所有字符串被看作文件名。搜索的结果被送到屏幕,不影响原文件内容。

首先创建一个演示文档用于后续对 grep 命令的讲解演示:

$ cat demo_file
THIS LINE IS THE 1ST UPPER CASE LINE IN THIS FILE.
this line is the 1st lower case line in this file.
This Line Has All Its First Character Of The Word With Upper Case.

Two lines above this line is empty.
And this is the last line.

1、在单个文件中搜索特定的字符串,输出为包含特定字符串的文本行

Syntax:
grep "literal_string" filename

$ grep "this" demo_file
this line is the 1st lower case line in this file.
Two lines above this line is empty.

2、在多个文件中检索指定的字串,输出为包含特定字符串的文件名和文本行

Syntax:
grep "string" FILE_PATTERN

为了演示需要先将刚才的演示文档复制一份。输出的文本行前面会加上相应的文件名。

$ cp demo_file demo_file1

$ grep "this" demo_*
demo_file:this line is the 1st lower case line in this file.
demo_file:Two lines above this line is empty.
demo_file:And this is the last line.
demo_file1:this line is the 1st lower case line in this file.
demo_file1:Two lines above this line is empty.
demo_file1:And this is the last line.

3、利用 grep -i 进行大小写无关的搜索

Syntax:
grep -i "string" FILE

将忽略大小写的区别,这样的话,字符串“the”,“The”和“THE”都将被搜索。

$ grep -i "the" demo_file
THIS LINE IS THE 1ST UPPER CASE LINE IN THIS FILE.
this line is the 1st lower case line in this file.
This Line Has All Its First Character Of The Word With Upper Case.
And this is the last line.

4、使用用正则表达式

Syntax:
grep "REGEX" filename

例如,要搜索全部以“lines”开始以“empty”结束的字串,如搜索“lines[之间任意字]empty”,并且忽略大小写:

$ grep -i "lines.*empty" filename //中间的点不能省略

正则表达式遵循的几个重复的操作:

  • ? 最多匹配一次
  • * 匹配零次或者任意多次
  • + 匹配一次以上
  • {n} 匹配n次
  • {n,} 最少匹配n次
  • {,m} 最多匹配m次
  • {n,m} 匹配n到m次

5、用 grep -w 搜索整个词,而不是词中的部分字符串

搜索包含单词为“is”的文本行,当然包含“his”, “this” 等的文本行将忽略:

$ grep -i "is" demo_file
THIS LINE IS THE 1ST UPPER CASE LINE IN THIS FILE.
this line is the 1st lower case line in this file.
This Line Has All Its First Character Of The Word With Upper Case.
Two lines above this line is empty.
And this is the last line.

6、利用 grep -A -B -C 显示所匹配的文本行的前/后/前后一些行内容

对于一些大型文件,不仅显示所匹配的文本行,而且显示之前/之后/前后的一些文本行也是很有用的。为演示需要,先创建另一个演示文档:

$ cat demo_text
 4. Vim Word Navigation

 You may want to do several navigation in relation to the words, such as:

  * e - go to the end of the current word.
  * E - go to the end of the current WORD.
  * b - go to the previous (before) word.
  * B - go to the previous (before) WORD.
  * w - go to the next word.
  * W - go to the next WORD.

 ORD - WORD consists of a sequence of non-blank characters, separated with white space.
 word - word consists of a sequence of letters, digits and underscores.

 Example to show the difference between WORD and word

  * 192.168.1.1 - single WORD
  * 192.168.1.1 - seven words.

6.1、显示匹配行之后的行 -A(After)

Syntax:
grep -A <N> "string" FILENAME

例如,显示匹配行之后的三行内容(空行也算):

$ grep -A 3 -i "example" demo_text
Example to show the difference between WORD and word

* 192.168.1.1 - single WORD
* 192.168.1.1 - seven words.

6.2、显示匹配行之前的行 -B(Before)

Syntax:
grep -B <N> "string" FILENAME

例如,显示匹配行之前的两行内容(空行也算):

$ grep -B 2 "single WORD" demo_text
Example to show the difference between WORD and word

* 192.168.1.1 - single WORD

6.3、显示匹配行前后的行 -C

例如,显示匹配行前后的各两行内容(空行也算):

$ grep -C 2 "Example" demo_text
word - word consists of a sequence of letters, digits and underscores.

Example to show the difference between WORD and word

* 192.168.1.1 - single WORD

7、高亮显示匹配的内容

需要设置 GREP_OPTIONS 环境变量:

$ export GREP_OPTIONS='--color=auto' GREP_COLOR='100;8'

然后再进行搜索。

$ export GREP_OPTIONS='--color=auto' GREP_COLOR='100;8'

$ grep this demo_file
this line is the 1st lower case line in this file.
Two lines above this line is empty.
And this is the last line.

8、利用 grep -r 在目录中递归搜索

如果在目录中即有文件又有目录,则普通的搜索只搜索文件而不搜索目录,利用 -r 选项即可递归进行搜索,搜索目录下的所有文件及其子目录内的文件。

$ grep -r "ramesh" *

9、利用 grep -v 搜索不包含特定字符串的文本行

例如显示所有不包含字符串“go”的文本行:

$ grep -v "go" demo_text
4. Vim Word Navigation

You may want to do several navigation in relation to the words, such as:

WORD - WORD consists of a sequence of non-blank characters, separated with white space.
word - word consists of a sequence of letters, digits and underscores.

Example to show the difference between WORD and word

* 192.168.1.1 - single WORD
* 192.168.1.1 - seven words.

10、显示不包含所有几个特定字符串的文本行

Syntax:
grep -v -e "pattern" -e "pattern"


$ cat test-file.txt
a
b
c
d

$ grep -v -e "a" -e "b" -e "c" test-file.txt
d

11、利用grep -c统计所匹配的字符串数目

Syntax:
grep -c "pattern" filename

例如:

$ grep -c "go" demo_text
6

查找有多少行匹配字符串:

$ grep -c this demo_file
3

查找有多少行不匹配字符串:

$ grep -v -c this demo_file
4

12、利用 grep -l 只显示包含特定字符串的文件名称

$ grep -l this demo_*
demo_file
demo_file1

13、利用 grep -o 只显示所匹配的字符串

默认的搜索显示的是匹配的文本行的所有内容,但是如果想只显示所搜的特定字符串,则就要使用 -o 选项了。乍看起来只显示所匹配的字符串好像用处不大,但在使用正则表达式的搜索中很有用处。

$ grep -o "is.*line" demo_file
is line is the 1st lower case line
is line
is is the last line

14、显示特定字符串的具体位置

Syntax:
grep -o -b "pattern" file

例如:

$ cat temp-file.txt
12345
12345

$ grep -o -b "3" temp-file.txt
2:3
8:3

15、利用 grep -n 同时显示行号

$ grep -n "go" demo_text
5: * e - go to the end of the current word.
6: * E - go to the end of the current WORD.
7: * b - go to the previous (before) word.
8: * B - go to the previous (before) WORD.
9: * w - go to the next word.
10: * W - go to the next WORD.
回顶部