grep递归查找头文件_Grep命令教程–如何使用递归查找在Linux和Unix中搜索文件

本文详细介绍了Linux和Unix系统中grep命令的用法,包括如何使用不同选项进行递归搜索、列出行号、打印匹配行数、忽略大小写等。通过示例展示了如何结合正则表达式进行文件内容查找,帮助用户更有效地在终端中搜索和定位文件。
摘要由CSDN通过智能技术生成

grep递归查找头文件

grep stands for Globally Search For Regular Expression and Print out. It is a command line tool used in UNIX and Linux systems to search a specified pattern in a file or group of files.

grep代表全局搜索正则表达式并打印出来 。 它是UNIX和Linux系统中使用的命令行工具,用于搜索文件或文件组中的指定模式。

grep comes with a lot of options which allow us to perform various search-related actions on files. In this article, we'll look at how to use grep with the options available as well as basic regular expressions to search files.

grep带有许多选项,使我们可以对文件执行各种与搜索相关的操作。 在本文中,我们将研究如何将grep与可用选项以及基本正则表达式一起使用来搜索文件。

如何使用grep (How to use grep)

Without passing any option, grep can be used to search for a pattern in a file or group of files. The syntax is:

在不传递任何选项的情况下,可以使用grep搜索文件或文件组中的模式。 语法为:

grep '<text-to-be-searched>' <file/files>

Note that single or double quotes are required around the text if it is more than one word.

请注意,如果文本超过一个单词,则必须在文本周围加上单引号或双引号。

You can also use the wildcard (*) to select all files in a directory.

您也可以使用通配符(*)选择目录中的所有文件。

The result of this is the occurences of the pattern (by the line it is found) in the file(s). If there is no match, no output will be printed to the terminal.

结果是文件中出现了模式(通过找到的行)。 如果不匹配,则不会将任何输出打印到终端。

For example, say we have the following files (called grep.txt):

例如,假设我们有以下文件(称为grep.txt):

Hello, how are you
I am grep
Nice to meet you

The following grep command will search for all occurences of the word 'you':

以下grep命令将搜索单词“ you”的所有出现:

grep you grep.txt

The result for this is:

结果是:

Hello, how are you
Nice to meet you

you is expected to have a different color than the other text to easily identify what was searched for.

you应该使用与其他文字不同的颜色,以轻松识别所搜索的内容。

But grep comes with more options which help us achieve more during a search operation. Let's look at nine of them while applying them to the example above.

但是grep带有更多选项,可帮助我们在搜索操作中实现更多目标。 在将它们应用于上面的示例时,让我们看一下其中的九个。

grep使用的选项 (Options used with grep)

1. -n (--line-number)-列出行号 (1. -n (--line-number) - list line numbers)

This prints out the matches for the text along with the line numbers. If you look at the result we have above, you'll notice there are no line numbers, just the matches.

这会打印出文本的匹配项以及行号。 如果看上面的结果,您会发现没有行号,只有匹配项。

grep you grep.txt -n

Result:

结果:

1: Hello, how are you
3: Nice to meet you
2. -c (--count)-打印匹配的行数 (2. -c (--count) - prints the number of lines of matches)
grep you grep.txt -c

Result:

结果:

2

Note that if there was another 'you' on line one, option -c would still print 2. This is because it is concerned with the number of lines where the matches appear, not the number of matches.

请注意,如果第一行上还有另一个“您”,则选项-c仍会打印2。这是因为它与匹配项出现的行数有关,而不是匹配数。

3. -v (--invert-match)-打印与指定模式不匹配的行 (3. -v (--invert-match) - prints the lines that do not match the specified pattern)
grep you grep.txt -v -n

Result:

结果:

2. I am grep

Notice that we also used option -n? Yes, you can apply multiple options in one command.

注意,我们还使用了-n选项。 是的,您可以在一个命令中应用多个选项。

4. -i (--ignore-case)-用于不区分大小写 (4. -i (--ignore-case) - used for case insensitivity)
# command 1
grep You grep.txt
# command 2
grep YoU grep.txt -i

Results:

结果:

# result 1
# no result
# result 2
Hello, how are you
Nice to meet you
5. -l (--files-with-matches)-打印与模式匹配的文件名 (5. -l (--files-with-matches) - print file names that match a pattern)
# command 1
grep you grep.txt -l
# command 2
grep You grep.txt -i -l

Results:

结果:

# result 1
grep.txt
# result 2
# all files in the current directory that matches
# the text 'You' case insensitively
#### 6. `-w` (--word-regexp) - print matches of the whole word

By default, grep matches strings which contain the specified pattern. This means that grep yo grep.txt will print the same results as grep yo grep.txt because 'yo' can be found in you. Similarly, 'ou'.

默认情况下, grep匹配包含指定模式的字符串。 这意味着grep yo grep.txt将打印与grep yo grep.txt相同的结果,因为可以在您中找到“ yo”。 同样,“ ou”。

With the option -w, grep ensures that the matches are exactly the same pattern as specified. Example:

使用-w选项, grep确保匹配项与指定的模式完全相同。 例:

grep yo grep.txt -w

Result:

结果:

No result!

没有结果!

7. -o (--only-matching)-仅打印匹配的图案 (7. -o (--only-matching) - print only the matched pattern)

By default, grep prints the line where the matched pattern is found. With option -o, only the matched pattern is printed line by line. Example:

默认情况下, grep打印找到匹配模式的行。 使用选项-o ,仅匹配的图案逐行打印。 例:

grep yo grep.txt -o

Result:

结果:

yo
8. -A (--context)和-B (--before-context)-在(分别)匹配的模式之后和之前打印行 (8. -A (--after-context) and -B (--before-context) - print the lines after and before (respectively) the matched pattern)
grep grep grep.txt -A 1 -B 1

Result:

结果:

Hello, how are you
I am grep
Nice to meet you

This matched pattern is on line 2. -A 1 means one line after the matched line and -B 1 means one line before the matched line.

此匹配的模式在第2行上。- -A 1表示匹配线之后的一行, -B 1表示匹配线之前的一行。

There's also a -C (--context) option which is equal to -A + -B. The value passed to -C would be used for -A and -B.

还有一个-C (--context)选项,它等于-A + -B 。 传递给-C的值将用于-A-B

9. -R (--dereference-recursive)-递归搜索 (9. -R (--dereference-recursive) - recursive search)

By default, grep cannot search directories. If you try doing so, you'll get an error ("Is a directory"). With option -R, searching files within directories and subdirectories becomes possible. Example:

默认情况下, grep无法搜索目录。 如果尝试这样做,将会收到错误消息(“是目录”)。 使用选项-R ,可以在目录和子目录中搜索文件。 例:

grep you .

Result:

结果:

# 'you' matches in a folders
# and files starting from the
# current directory

模式的正则表达式 (Regular expressions for patterns)

grep also allows basic regular expressions for specifying patterns. Two of them are:

grep还允许用于指定模式的基本正则表达式。 其中两个是:

1. ^pattern一行的开始 (1. ^pattern - start of a line)

This pattern means that the grep will match the strings whose lines begin with the string specified after ^. Example:

此模式意味着grep将匹配其行以^之后指定的字符串开头的字符串。 例:

grep ^I grep.txt -n

Result:

结果:

2: I
2. pattern$ -行尾 (2. pattern$ - end of a line)

In contrast with ^, $ specifies patterns that will be matched if the line ends with the string before $. Example:

^相反, $指定如果行以$之前的字符串结尾的模式将匹配。 例:

grep you$ grep.txt

Result:

结果:

1: Hello, how are you
3: Nice to meet you

结语 (Wrap up)

grep is a powerful tool for searching files in the terminal. Understanding how to use it gives you the ability to easily find files via the terminal.

grep是用于在终端中搜索文件的强大工具。 了解如何使用它使您能够通过终端轻松查找文件。

There are more options attached to this tool. You can find with man grep.

此工具附带了更多选项。 您可以与man grep一起找到。

翻译自: https://www.freecodecamp.org/news/grep-command-tutorial-how-to-search-for-a-file-in-linux-and-unix/

grep递归查找头文件

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值