Linux grep命令

2. grep

在文件中查找指定字符串(大小写敏感)
$ grep -"the" demo_file
输出匹配的行和后面三行内容
$ grep -3 -"example" demo_text
递归搜索目录,查找指定字符串
$ grep -"ramesh" *
/*****************************************************************************************************************/

你需要掌握 Linux grep 命令的用法,在这篇文章中,我们将通过 15 个 grep 命令例子来学习 Linux/Unix 中 grep 命令的用法。

首先我们需要创建本文章中 grep 命令例子需要使用的 'demo_file':

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. 在单个文件找查找指定字符

grep 命令最基本的用法是在单个文件查找指定字符串,语法:

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.
And this is the last line.

2. 在多个文件里查找指定字符串

语法:

grep "string" FILE_PATTERN

着同样是 grep 的基本用法,比如,我们拷贝 'demo_file' 到 'demo_file1'。grep 命令的输出会在每行开头包含匹配模式的文件名。命令中的通配符会由Linux/Unix处理,会将匹配的文件提供给 grep 进行处理。

$ 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.

搜索时不区分大小写

使用 -i 选项可以在搜索时不区分大小写,语法:

grep -"string" FILE

着同样也是 grep 的基本用法,按照特定的字符串/模式搜索目标文件,不区分大小写。因此,下面命令可以匹配如: 'the', 'THE' 以及 'tHe' 这样的字符串。

$ grep -"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.匹配正则表达式

如果你精通正则表达式的话,这会是 grep 的一个非常强大的功能,语法如下:

grep "REGEX" filename

下面的例子,查找 'lines' 和 'empty' 间的字符串:

$ grep "lines.*empty" demo_file
Two lines above this line is empty.

来自于 grep 文档的描述:正则表达式可能包含一些重复操作符:

  • ?: 该符号前面的元素是可选的并且至多匹配一次。

  • *: 该符号前面的元素可以匹配 0 至多次

  • +: 该符号前面的元素可以匹配 1 至多次

  • {n}: 该符号前面的元素可以只能匹配 n 次

  • {n,}: 该符号前面的元素可以匹配 n 到多次

  • {,m}: 该符号前面的字符最多匹配 m 次

  • {n,m}: 前面的字符只能匹配 n 至 m 次

5. 使用 -w 按字查找(不查找子串)

如果你希望按字进行搜索,而不希望搜索到子串,可以使用 -w 选项。下面的例子是使用正则表达式进行搜索,搜索的目标是 'is' 字符产。如果不使用 -w 参数,结果中会包含 'is', 'his', 'this' 以及所有包含 'is' 的字符串。

$ grep -"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.

下面的例子是使用 grep 按字搜索,仅查找 'is':

$ grep -iw "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.
Two lines above this line is empty.
And this is the last line.

6. 显示匹配结果的前面/后面/周围的内容

当在一个大文件中查找时,能够看到匹配内容相关的前/后内容是非常有帮助的。在这这个例子中我们需要创建一个新测试文件:

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.

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.

6.1 显示匹配项以及后面 N 行内容

使用 -A 选项来显示匹配项以及后面 N 行内容,语法如下:

grep -<N> "string" FILENAME

下面的例子显示匹配项以及后面 3 行内容:

$ grep -3 -"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 显示匹配项以及前面 N 行内容

使用 -B 选项可以显示匹配项,以及前面 N 行内容,语法:

grep -<N> "string" FILENAME

示例:

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

* 192.168.1.1 - single WORD

6.3 显示匹配项以及周围的 N 行内容

使用使用 -C 选项显示匹配项以及匹配项周围(前面、后面)的 N 行内容:

$ grep -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 是根据字符串/模式来进行搜索,如果你想高亮搜索结果中那一部分是匹配搜索词的,可以使用 GREP_OPTIONS 选项,比如:

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. 递归搜索所有文件

如果想要搜索当前文件夹以及所有子文件夹中的文件,可以使用 -r 选项。下面的命令将在当前目录以及所有子目录的文件中搜索 '4byte.cn':

$ grep -"4byte.cn" *

9. 反向匹配(搜索不匹配的)

我们现在已经了解了很多搜索匹配内容的方法,当然,我们同样可以使用 -v 选项搜索不匹配给定字符串/模式的内容,下面的例子将显示所有匹配 'go' 的行:

$ grep -"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. 显示不匹配所有模式的内容

语法:

grep --"pattern" -"pattern"

示例:

cat test-file.txt
a
b
c
d

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

11. 计算匹配行个数

可以使用 -c 选项来计算匹配行的个数,语法如下:

grep -"pattern" filename

示例:

$ grep -"go" demo_text
6

计算匹配的行数:

$ grep -this demo_file
3

计算不匹配的行数:

$ grep --this demo_file
4

12. 仅显示匹配文件的文件名

如果你仅想知道有多少文件匹配搜索内容,可以使用 -l (小写 L)选项。

$ grep -this demo_*
demo_file
demo_file1

13. 仅显示匹配非字符串

默认情况下 grep 会显示匹配的整行内容,不过可以使用 -o 选项仅显示匹配的字符串,这个选项在直接使用字符串进行搜索时没有太大用处,不过在使用正则表达式搜索时,会变得非常强大,比如:

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

14. 显示匹配内容所在的位置

可以使用以下命令显示所有结果在文档中的位置,语法:

grep --"pattern" file

示例:

cat temp-file.txt
12345
12345

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

:上面输出的位置并不是在每行的位置,而是相对整个文件的偏移位置。

15. 在输出中显示行号

可以使用 -n 选项在输出中显示匹配结果所在的行号,如下例:

$ grep -"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.

/*****************************************************************************************************************/
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值