15 個對新手和專家都非常有用的 Linux Grep 命令示例
在本文中,讓我們了解15 個對新手和專家都非常有用的 Linux grep 命令的實際示例。

首先創建以下 demo_file,將在下面的示例中用于演示 grep 命令。
$ 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 命令的基本用法是在指定文件中搜索特定字符串,如下所示。
Syntax: 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. 在多個文件中檢查給定的字符串。
Syntax: grep "string" FILE_PATTERN
這也是 grep 命令的基本用法。在本例中,讓我們將 demo_file 復制到 demo_file1。grep 輸出還將在與特定模式匹配的行前面包含文件名,如下所示。當 Linux shell 看到元字符時,它會進行擴展并將所有文件作為輸入提供給 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.
3. 不區分大小寫的搜索使用 grep -i
Syntax: grep -i "string" FILE
這也是grep的基本用法。這不區分大小寫地搜索給定的字符串/模式。因此,它不區分大小寫地匹配所有單詞,例如“the”、“THE”和“The”,如下所示。
$ grep -i "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. 匹配文件中的正則表達式
Syntax: grep "REGEX" filename
這是一個非常強大的功能,如果您可以有效地使用正則表達式。在下面的示例中,它搜索所有以“lines”開頭并以“empty”結尾的模式,以及中間的任何內容。即在 demo_file 中搜索“lines[anything in-between]empty”。
$ grep "lines.*empty" demo_file Two lines above this line is empty.
來自 grep 的文檔:正則表達式后面可以跟幾個重復運算符之一:
??前一項是可選的,最多匹配一次。
* 前面的項目將匹配零次或多次。
+ 前一項將匹配一次或多次。
{n} 前一項正好匹配 n 次。
{n,} 前一項被匹配 n 次或更多次。
{,m} 前一項最多匹配 m 次。
{n,m} 前一項至少匹配 n 次,但不超過 m 次。
5. 使用 grep -w 檢查完整的單詞,而不是子字符串
如果您想搜索一個詞,并避免它與子字符串匹配,請使用 -w 選項。只需進行正常搜索即可顯示所有行。
以下示例是常規 grep,它在其中搜索“is”。當您搜索“is”時,如果沒有任何選項,它將顯示“is”、“his”、“this”以及所有包含子字符串“is”的內容。
$ grep -i "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.
以下示例是 WORD grep,它僅搜索單詞“is”。請注意,此輸出不包含“This Line Has All its First Character Of The Word With Big Case”這一行,即使“This”中有“is”,因為以下僅查找單詞“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. 使用 grep -A、-B 和 -C 顯示比賽前/后/周圍的行
在對大文件執行 grep 時,在匹配后查看一些行可能很有用。如果 grep 不僅可以顯示匹配行,還可以顯示匹配之后/之前/周圍的行,您可能會覺得很方便。
請為此示例創建以下 demo_text 文件。
$ 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.
-A 是在匹配后打印指定的 N 行的選項,如下所示。
Syntax: grep -A
以下示例打印匹配的行及其后的 3 行。
$ grep -A 3 -i "example" demo_text Example to show the difference between WORD and word * 192.168.1.1 - single WORD * 192.168.1.1 - seven words.
-B 是在匹配之前打印指定的 N 行的選項。
Syntax: grep -B
當您可以選擇在匹配后顯示 N 行時,您可以使用 -B 選項。
$ grep -B 2 "single WORD" demo_text Example to show the difference between WORD and word * 192.168.1.1 - single WORD
-C 是在匹配之前打印指定的 N 行的選項。在某些情況下,您可能希望匹配顯示為兩側的線條。此選項在匹配的兩側(之前和之后)顯示 N 行。
$ grep -C 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_OPTIONS 突出顯示搜索
由于 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.使用grep -r遞歸搜索所有文件
當您要搜索當前目錄及其子目錄下的所有文件時。-r 選項是您需要使用的選項。以下示例將在當前目錄及其所有子目錄中的所有文件中查找字符串“ramesh”。
$ grep -r "ramesh" *
9. 使用 grep -v 反轉匹配
您有不同的選項來顯示匹配的行、顯示匹配前的行、顯示匹配后的行以及突出顯示匹配。因此,您肯定還希望選項 -v 進行反轉匹配。
如果要顯示與給定字符串/模式不匹配的行,請使用選項 -v,如下所示。此示例將顯示與單詞“go”不匹配的所有行。
$ grep -v "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.顯示不匹配所有給定模式的行。
Syntax: grep -v -e "pattern" -e "pattern"
$ cat test-file.txt a b c d $ grep -v -e "a" -e "b" -e "c" test-file.txt d
11.使用grep -c計算匹配的數量
當您想計算與給定模式/字符串匹配的行數時,請使用選項 -c。
Syntax: grep -c "pattern" filename
$ grep -c "go" demo_text 6
當你想找出多少行與模式匹配時
$ grep -c this demo_file 3
當您想要找出與模式不匹配的行數時
$ grep -v -c this demo_file 4
12. 使用 grep -l 僅顯示與給定模式匹配的文件名
如果您希望 grep 僅顯示與給定模式匹配的文件名,請使用 -l(小寫 L)選項。
當您將多個文件作為輸入提供給 grep 時,它會顯示包含與模式匹配的文本的文件名,當您嘗試在整個目錄結構中查找一些注釋時會非常方便。
$ grep -l this demo_* demo_file demo_file1
13.只顯示匹配的字符串
默認情況下,grep 將顯示與給定模式/字符串匹配的行,但如果您希望 grep 僅顯示模式的匹配字符串,請使用 -o 選項。
當您直接給出字符串時,它可能沒有那么有用。但是當您提供正則表達式模式并嘗試查看它匹配的內容時,它變得非常有用
$ grep -o "is.*line" demo_file is line is the 1st lower case line is line is is the last line
14.顯示匹配在行中的位置
當您希望 grep 顯示與文件中的模式匹配的位置時,請使用以下選項作為
Syntax: grep -o -b "pattern" file
$ cat temp-file.txt 12345 12345 $ grep -o -b "3" temp-file.txt 2:3 8:3
注意:上面grep命令的輸出不是行中的位置,而是整個文件的字節偏移量。
15.使用grep -n顯示輸出時顯示行號
顯示與行匹配的文件的行號。它對每個文件進行基于 1 的行編號。使用 -n 選項來利用此功能。
$ grep -n "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.
grep Linux 專家
版權聲明:本文內容由網絡用戶投稿,版權歸原作者所有,本站不擁有其著作權,亦不承擔相應法律責任。如果您發現本站中有涉嫌抄襲或描述失實的內容,請聯系我們jiasou666@gmail.com 處理,核實后本網站將在24小時內刪除侵權內容。
版權聲明:本文內容由網絡用戶投稿,版權歸原作者所有,本站不擁有其著作權,亦不承擔相應法律責任。如果您發現本站中有涉嫌抄襲或描述失實的內容,請聯系我們jiasou666@gmail.com 處理,核實后本網站將在24小時內刪除侵權內容。