
在 Linux 世界中,搜索文本文件以查找特定内容是一项常见任务,而使用 grep 命令可以高效地完成这项任务。grep 是 “Global Regular Expression Print(全局正则表达式打印)” 的缩写,是一种功能强大的命令行工具,允许用户使用正则表达式定义的模式搜索文件。
无论是查找日志文件中的特定错误,还是在大型代码库中查找特定术语的所有实例, grep 都是文本搜索和操作的首选工具。grep 可以匹配复杂的模式、过滤结果,甚至可以跨多个文件执行操作,是系统管理员、程序员和数据分析师的重要工具。
grep 命令的一般语法
$ grep [OPTIONS...] [PATTERN] [FILE...]
1. 搜索文件中的内容
grep exp FileName.txt
grep 是一个功能强大的命令,它允许你搜索一个文件或多个文件中存在的一组特定字符或单词。上面的命令搜索 FileName.txt 中的 exp,找到后返回结果。
注: grep  默认区分大小写,如果没有其他参数,只要与 “exp” 匹配 grep 就会返回结果。
示例:
假设 FileName.txt 包含以下文本:
This is an example file. The word exp is here. No match in this line. Expression is a good word. Experience teaches wisdom.
命令 grep exp FileName.txt 的输出结果如下:
This is an example file. The word exp is here. Expression is a good word. Experience teaches wisdom.
此输出将显示 FileName.txt 中包含子串 “exp” 的所有行。
2. 在多个文件中搜索内容
grep all name1.txt name2.txt name3.txt
该命令将搜索范围扩展到多个指定文件名。
示例:
命令 grep all name1.txt name2.txt name3.txt 使用 grep 在文件  name1.txt, name2.txt, 和 name3.txt 中搜索字符串 “all”。如果找到该字符串,将打印包含该字符串的行和文件名。
name1.txt:We are all in this together. name2.txt:All the best for your future. name3.txt:all of these lines match. name3.txt:All is well.
3. 用 grep 查找精确单词
grep -w example Example.txt
使用 -w 参数后,grep 的搜索会更加精确,只有当精确词匹配时才会返回 true。在上面的命令中,grep 在 Example.txt 中搜索 “example“。
以下任何一项都会返回 false:
- Example
- examples
4. 使用 grep 进行不区分大小写搜索
grep -i being ExampleFile.txt
使用 -i 参数后,grep 将以不区分大小写的方式进行搜索,只要输入的内容匹配,不管是小写还是大写字母,都将返回 true。
上面的命令在 ExampleFile.txt 中搜索单词 “being“,如果找到将返回结果。
如果存在 -i,以下所有命令都将返回 true:
- “Being”
- “beING“
5. 用 grep 计算和输出单词重复率
grep -c smallness TextFile.txt
使用 -c 参数后,grep 会首先查找是否存在特定单词,然后计算该单词的重复次数。上面的命令搜索 “smallness“,并返回它在 TextFile.txt 中出现的次数。
下面是给定命令的假设输出示例:
5
这意味着在 TextFile.txt 文件的在 5 行中找到了 “smallness” 一词。如果在文件中根本找不到 “smallness” 一词,命令将输出
0
6. 使用 grep 进行反向搜索
grep -v lorem sometext.txt
参数 -v 将排除与输入模式匹配的整行,并输出不包含该模式的其余部分。上述命令在 sometext.txt 中搜索 “lorem“。任何不含 “lorem” 的行都将返回 true。
示例:
假设 sometext.txt 包含以下行:
lorem ipsum dolor sit amet consectetur adipiscing elit lorem sed do eiusmod tempor
如果运行 grep -v 'lorem' sometext.txt 命令,输出结果将是
consectetur adipiscing elit
只有这一句没有 “lorem” 一词。
7. 显示匹配行和列表行号
grep -n ipsum randomtext.txt
参数 -n 会返回包含行数的内容。如果包含搜索单词,则返回整行(单词存在的地方)及其行数。上面的命令在 randomtext.txt 中搜索 “ipsum“,其输出显示了 “ipsum“所在的行。
示例:
假设 randomtext.txt 有以下内容:
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Another line without the search term. Yet another line. ipsum ipsum ipsum Here's an ipsum too.
命令 grep -n ipsum randomtext.txt 将产生以下结果:
1:Lorem ipsum dolor sit amet, consectetur adipiscing elit. 4:ipsum ipsum ipsum 5:Here's an ipsum too.
这里,冒号前的数字代表文件中找到字符串 “ipsum” 的行号。
8. 列出包含匹配字符串的文件名
grep -l dolor *txt
使用 -l 参数时,只有包含 “dolor” 的 .txt 扩展名文件才会返回 true。文件名将被打印出来,而不是整个文件。
示例:
假设目录中有三个文件,即 file1.txt, file2.txt, 和 file3.txt,并且在 file1.txt 和 file3.txt 中发现了 “dolor”,那么输出结果将如下所示:
file1.txt file3.txt
9. 搜索以单一规则条件开头的行
grep ^Example TextFile.txt
搜索规则前面的字符 ^ 表示 grep 只能搜索以搜索规则开头的单词,而不能搜索其他单词。上面的命令将搜索 TextFile.txt,并返回所有以 “Example” 开头的行。
示例:
假设 TextFile.txt 包含以下文本:
Example line 1 This is another line Example line 2 Yet another line without the keyword Example line 3
该命令的输出将是:
Example line 1 Example line 2 Example line 3
10. 使用 grep 进行多规则匹配搜索
| 1 | grep-e lorem -e amet ExampleFile.txt | 
在同一命令中,-e 参数可以多次使用;每一次都与搜索规则配对,可以让你在搜索文件时更有针对性。上面的命令在 ExampleFile.txt 中搜索 “lorem” 和 “amet“,如果为真/找到则返回。
示例:
假设 ExampleFile.txt 包含以下行:
lorem ipsum dolor sit amet consectetur adipiscing elit amet, consectetur adipiscing sed do eiusmod tempor lorem incididunt ut
运行命令 grep -e lorem -e amet ExampleFile.txt 将输出结果:
lorem ipsum dolor sit amet amet, consectetur adipiscing lorem incididunt ut
更多 Linux 命令
下面罗列了最常见的一些 Linux 命令,您可以根据自己的需要查阅对应命令的详细解析:
| 目录操作 | rmdir·cd·pwd·exa·ls | 
| 文件操作 | cat·cp·dd·less·touch·ln·rename·more·head | 
| 文件系统操作 | chown·mkfs·locate | 
| 网络 | ping·curl·wget·iptables·mtr | 
| 搜索和文本处理 | find·grep·sed·whatis·ripgrep·fd·tldr | 
| 系统信息和管理 | env·history·top·who·htop·glances·lsof | 
| 用户和会话管理 | screen·su·sudo·open | 
此外,我们还整理 Linux 命令行大全,以帮助大家全面深入地学习 Linux。
















暂无评论内容