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遞歸查找頭文件