说明
本文是个人学习用的非官方中英对照译文,原文:https://diveintosystems.org/book/Appendix2/grep.html。
原书采用 CC BY-NC-ND 4.0 许可(禁止演绎),请勿公开传播本译文。
标为「译注」「现代更新」的内容是译者补充,不代表原书作者观点;代码与原文逐字一致,未作改动。
17.6. Searching: grep and find
一项常见的任务是搜索特定的文件,或者在文件中搜索特定的内容。find 命令适合查找名字符合特定模式的文件或目录,而 grep 命令适合在一个文件或一组文件中搜索特定的模式。
A common task is to search for specific files or for specific content within files. The
findcommand is useful for finding a file or directory whose name has a specific pattern, whereas thegrepcommand is useful for searching for a specific pattern within a file or set of files.
17.6.1. find 命令
17.6.1. The find Command
find 命令(从目录结构中给定的起点开始)对文件系统进行递归搜索,查找任何文件。使用 -name <search pattern> 命令行选项运行时,find 搜索名字与给定搜索模式匹配的所有文件(和目录)。搜索模式是一个正则表达式,它包含字符字面量,以及指定一般模式的特殊字符或子模式。正则表达式中常用的一个特殊字符是 *,它的意思是「匹配零个或多个任意字符」。所以对于搜索模式 "hello*",hello1、hellothere 和 hello_123_XYZ 都与之匹配。下面是用 find 命令查找文件的几个例子:
The
findcommand performs a recursive search of a filesystem (at a given start point in the directory structure), to find any files. When run with the-name <search pattern>command line option,findsearches for all files (and directories) with a name matching the given search pattern. The search pattern is a regular expression that includes character literals and special characters or sub-patterns that specify general patterns. One special character commonly used in regular expressions is `*`. It means “match with zero or more of any character”. So for the search pattern"hello*",hello1,hellothere, andhello_123_XYZall match the pattern. Here are a few examples of thefindcommand to find files:
译注
严格说来,find -name 使用的是 shell 风格的通配符(glob)模式,而不是正则表达式(man find 中 -name 的说明写的是「special shell pattern matching characters」);在通配符里 * 本身就表示「零个或多个任意字符」,而在正则表达式里 * 表示「前一个字符重复零次或多次」(下面 grep 的例子用的就是后一种含义)。
1 | $ find ./ -name dog.c # from current directory (./) find all files named dog.c |
find 命令还有许多其他命令行选项,让用户能以不同的方式配置它的搜索行为。更多信息参见它的手册页。
There are many other command line options to the
findcommand, allowing users to configure its search behavior in different ways. See its man page for more information.
17.6.2. grep 命令
17.6.2. The grep Command
grep 命令在一个文件或一组文件中查找模式。它可以根据模式匹配把数据从源过滤到目标。grep 命令接收两个命令行参数:一个作为搜索模式的正则表达式,以及要在其中搜索匹配模式的一个文件(或一组文件)。它输出文件(或这组文件)中每一个含有该模式匹配项的行。与 find 一样,grep 也有许多命令行选项来定制它的搜索。下面是用 grep 命令在文件中查找模式的几个例子。这些例子中用到的文件之一是 temp 文件,它的内容如下:
The
grepcommand finds patterns within a file or within a set of files. It can filter data from a source to a destination based on pattern matching. Thegrepcommand takes two command line arguments, a search pattern regular expression, and a file (or set of files) to search in for matching patterns. It outputs every line in the file (or set of files) that has a matching occurrence of the pattern. Likefind,grepalso has many command line options to tailor its search. Below are a few examples of thegrepcommand to find patterns in a file(s). One of the files used in these examples is thetempfile, whose contents are the following:
1 | hello |
- 在文件
temp中搜索"hello"的所有出现:1
2
3
4$ grep hello temp
hello
hello there
hello There
Search for all occurrences of
"hello"in the filetemp:
1
2
3
4 $ grep hello temp
hello
hello there
hello There
- 在文件
temp中搜索"hello there"的出现。在这个例子中,由于搜索模式中有空格字符,模式需要放在双引号中。1
2$ grep "hello there" temp
hello there
Search for occurrences of
"hello there"in the filetemp. In this example, because the search pattern has space characters in it, the pattern needs to be inside double quotes.
1
2 $ grep "hello there" temp
hello there
- 在匹配结果中包含文件的行号(
-n命令行选项),并在模式中忽略大小写(-i命令行选项):1
2
3$ grep -n -i "hello there" temp
4:hello there
5:hello There
Include the file line numbers with matches (
-ncommand line option) and ignore case in the pattern (-icommand line option):
1
2
3 $ grep -n -i "hello there" temp
4:hello there
5:hello There
- 搜索所有含有任意一个大写字母(
[A-Z])后跟el的模式:1
2
3
4$ grep "[A-Z]el" temp
Hello
hiHelo
Mellow
Search for all patterns that have any upper-case alphabetic character (
[A-Z]) followed by anel:
1
2
3
4 $ grep "[A-Z]el" temp
Hello
hiHelo
Mellow
- 搜索所有以任意一个大写字母(
[A-Z])开头(\b匹配单词边界)、后跟el的单词:1
2
3$ grep "\b[A-Z]el" temp
Hello
Mellow
Search for all words that start with (
\bmatches a word boundary) any upper-case alphabetic character ([A-Z]) followed by anel:
1
2
3 $ grep "\b[A-Z]el" temp
Hello
Mellow
- 搜索所有含有任意一个大写字母(
[A-Z])后跟零个或多个(*)e字符的模式:注意,1
2
3
4
5
6$ grep "[A-Z]e*" temp
Hello
hiHelo
hello There
Meow
Mellowhello There是一个匹配,因为它含有一个大写字符T,后跟零个e字符。
Search for all patterns that have any upper-case alphabetic character (
[A-Z]) followed by zero or more (`*`)echaracters:
1
2
3
4
5
6 $ grep "[A-Z]e*" temp
Hello
hiHelo
hello There
Meow
MellowNote that
hello Thereis a match because it contains an upper case character,T, followed by zeroecharacters.
- 搜索所有含有任意一个大写字母(
[A-Z])后跟el、再跟零个或多个(*)额外的l字符、再跟一个o的模式:1
2
3
4$ grep "[A-Z]ell*o" temp
Hello
hiHelo
Mellow
Search for all patterns that have any upper-case alphabetic character (
[A-Z]) followed byelfollowed by zero or more (`*`) additionallcharacters followed by ano:
1
2
3
4 $ grep "[A-Z]ell*o" temp
Hello
hiHelo
Mellow
- 在所有以
.c结尾的文件中搜索main:1
2
3
4
5
6$ grep main *.c
function.c:int main(void) {
hello.c:int main(void) {
string.c:int main(void) {
structfunc.c:int main(void) {
types_scanf.c:int main(void) {
Search for
mainin all files ending in.c:
1
2
3
4
5
6 $ grep main *.c
function.c:int main(void) {
hello.c:int main(void) {
string.c:int main(void) {
structfunc.c:int main(void) {
types_scanf.c:int main(void) {
- 在所有以
.c结尾的文件中搜索main,并列出所有匹配的文件名(-H)和行号(-n):1
2
3
4
5
6$ grep -H -n main *.c
function.c:21:int main(void) {
hello.c:13:int main(void) {
string.c:11:int main(void) {
structfunc.c:17:int main(void) {
types_scanf.c:13:int main(void) {
Search for
mainin all files ending in.c, and list the filename (-H) and the line number (-n) of all matches:
1
2
3
4
5
6 $ grep -H -n main *.c
function.c:21:int main(void) {
hello.c:13:int main(void) {
string.c:11:int main(void) {
structfunc.c:17:int main(void) {
types_scanf.c:13:int main(void) {
- 从
CS31子目录开始,在所有文件中递归地(-r)grep hello:1
$ grep -r hello ~/cs31
Recursively grep (
-r) for hello in all files starting in theCS31subdirectory:
1 $ grep -r hello ~/cs31
grep 有许多命令行选项,可以用不同的方式配置它的搜索。
There are many command line options to
grepto configure its search in different ways.
17.6.3. 参考资料
17.6.3. References
更多信息参见:
For more information see:
- 这些命令的手册页(例如
man grep)
- The man pages for these commands (e.g.,
man grep)
- gnu.org 上的 grep 用户信息
- grep user info from gnu.org
- cheat-sheets.org 上的最常用的 Unix 命令
- most used Unix commands from cheat-sheets.org
- gnu.org 上的 Bash 参考手册。
- Bash Reference Manual from gnu.org.