Linux運維基礎之grep命令詳解

Linux運維基礎之grep命令詳解

在 Linux 世界中,搜尋文字檔案以查詢特定內容是一項常見任務,而使用 grep 命令可以高效地完成這項任務。grep 是 “Global Regular Expression Print(全域性正規表示式列印)” 的縮寫,是一種功能強大的命令列工具,允許使用者使用正規表示式定義的模式搜尋檔案。

無論是查詢日誌檔案中的特定錯誤,還是在大型程式碼庫中查詢特定術語的所有例項, grep 都是文字搜尋和操作的首選工具。grep 可以匹配複雜的模式、過濾結果,甚至可以跨多個檔案執行操作,是系統管理員、程式設計師和資料分析師的重要工具。

grep 命令的一般語法

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
$ grep [OPTIONS...] [PATTERN] [FILE...]
$ grep [OPTIONS...] [PATTERN] [FILE...]
$ grep [OPTIONS...] [PATTERN] [FILE...]

1. 搜尋檔案中的內容

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
grep exp FileName.txt
grep exp FileName.txt
grep exp FileName.txt

grep 是一個功能強大的命令,它允許你搜尋一個檔案或多個檔案中存在的一組特定字元或單詞。上面的命令搜尋 FileName.txt 中的 exp,找到後返回結果。

注: grep  預設區分大小寫,如果沒有其他引數,只要與 “exp” 匹配 grep 就會返回結果。

示例:

假設 FileName.txt 包含以下文字:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
This is an example file.
The word exp is here.
No match in this line.
Expression is a good word.
Experience teaches wisdom.
This is an example file. The word exp is here. No match in this line. Expression is a good word. Experience teaches wisdom.
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 的輸出結果如下:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
This is an example file.
The word exp is here.
Expression is a good word.
Experience teaches wisdom.
This is an example file. The word exp is here. Expression is a good word. Experience teaches wisdom.
This is an example file.
The word exp is here.
Expression is a good word.
Experience teaches wisdom.

此輸出將顯示 FileName.txt 中包含子串 “exp” 的所有行。

2. 在多個檔案中搜尋內容

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
grep all name1.txt name2.txt name3.txt
grep all name1.txt name2.txt name3.txt
grep all name1.txt name2.txt name3.txt

該命令將搜尋範圍擴充套件到多個指定檔名。

示例:

命令 grep all name1.txt name2.txt name3.txt 使用 grep 在檔案  name1.txtname2.txt, 和 name3.txt 中搜尋字串 “all”。如果找到該字串,將列印包含該字串的行和檔名。

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
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.
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.
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 查詢精確單詞

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
grep -w example Example.txt
grep -w example Example.txt
grep -w example Example.txt

使用 -w 引數後,grep 的搜尋會更加精確,只有當精確詞匹配時才會返回 true。在上面的命令中,grepExample.txt 中搜尋 “example“。

以下任何一項都會返回 false:

  • Example
  • examples

4. 使用 grep 進行不區分大小寫搜尋

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
grep -i being ExampleFile.txt
grep -i being ExampleFile.txt
grep -i being ExampleFile.txt

使用 -i 引數後,grep 將以不區分大小寫的方式進行搜尋,只要輸入的內容匹配,不管是小寫還是大寫字母,都將返回 true。

上面的命令在 ExampleFile.txt 中搜尋單詞 “being“,如果找到將返回結果。

如果存在 -i,以下所有命令都將返回 true:

  • Being”
  • “beING

5. 用 grep 計算和輸出單詞重複率

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
grep -c smallness TextFile.txt
grep -c smallness TextFile.txt
grep -c smallness TextFile.txt

使用 -c 引數後,grep 會首先查詢是否存在特定單詞,然後計算該單詞的重複次數。上面的命令搜尋 “smallness“,並返回它在 TextFile.txt 中出現的次數。

下面是給定命令的假設輸出示例:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
5
5
5

這意味著在 TextFile.txt 檔案的在 5 行中找到了 “smallness” 一詞。如果在檔案中根本找不到 “smallness” 一詞,命令將輸出

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
0
0
0

6. 使用 grep 進行反向搜尋

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
grep -v lorem sometext.txt
grep -v lorem sometext.txt
grep -v lorem sometext.txt

引數 -v 將排除與輸入模式匹配的整行,並輸出不包含該模式的其餘部分。上述命令在 sometext.txt 中搜尋 “lorem“。任何不含 “lorem” 的行都將返回 true。

示例:

假設 sometext.txt 包含以下行:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
lorem ipsum dolor sit amet
consectetur adipiscing elit
lorem sed do eiusmod tempor
lorem ipsum dolor sit amet consectetur adipiscing elit lorem sed do eiusmod tempor
lorem ipsum dolor sit amet
consectetur adipiscing elit
lorem sed do eiusmod tempor

如果執行 grep -v 'lorem' sometext.txt 命令,輸出結果將是

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
consectetur adipiscing elit
consectetur adipiscing elit
consectetur adipiscing elit

只有這一句沒有 “lorem” 一詞。

7. 顯示匹配行和列表行號

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
grep -n ipsum randomtext.txt
grep -n ipsum randomtext.txt
grep -n ipsum randomtext.txt

引數 -n 會返回包含行數的內容。如果包含搜尋單詞,則返回整行(單詞存在的地方)及其行數。上面的命令在 randomtext.txt 中搜尋 “ipsum“,其輸出顯示了 “ipsum“所在的行。

示例:

假設 randomtext.txt 有以下內容:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
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.
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.
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 將產生以下結果:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
1:Lorem ipsum dolor sit amet, consectetur adipiscing elit.
4:ipsum ipsum ipsum
5:Here's an ipsum too.
1:Lorem ipsum dolor sit amet, consectetur adipiscing elit. 4:ipsum ipsum ipsum 5:Here's an ipsum too.
1:Lorem ipsum dolor sit amet, consectetur adipiscing elit.
4:ipsum ipsum ipsum
5:Here's an ipsum too.

這裡,冒號前的數字代表檔案中找到字串 “ipsum” 的行號。

8. 列出包含匹配字串的檔名

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
grep -l dolor *txt
grep -l dolor *txt
grep -l dolor *txt

使用 -l 引數時,只有包含 “dolor” 的 .txt 副檔名檔案才會返回 true。檔名將被列印出來,而不是整個檔案。

示例:

假設目錄中有三個檔案,即 file1.txtfile2.txt, 和 file3.txt,並且在 file1.txtfile3.txt 中發現了 “dolor”,那麼輸出結果將如下所示:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
file1.txt
file3.txt
file1.txt file3.txt
file1.txt
file3.txt

9. 搜尋以單一規則條件開頭的行

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
grep ^Example TextFile.txt
grep ^Example TextFile.txt
grep ^Example TextFile.txt

搜尋規則前面的字元 ^ 表示 grep 只能搜尋以搜尋規則開頭的單詞,而不能搜尋其他單詞。上面的命令將搜尋 TextFile.txt,並返回所有以 “Example” 開頭的行。

示例:

假設 TextFile.txt 包含以下文字:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
Example line 1
This is another line
Example line 2
Yet another line without the keyword
Example line 3
Example line 1 This is another line Example line 2 Yet another line without the keyword Example line 3
Example line 1
This is another line
Example line 2
Yet another line without the keyword
Example line 3

該命令的輸出將是:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
Example line 1
Example line 2
Example line 3
Example line 1 Example line 2 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 包含以下行:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
lorem ipsum dolor sit amet
consectetur adipiscing elit
amet, consectetur adipiscing
sed do eiusmod tempor
lorem incididunt ut
lorem ipsum dolor sit amet consectetur adipiscing elit amet, consectetur adipiscing sed do eiusmod tempor lorem incididunt ut
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 將輸出結果:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
lorem ipsum dolor sit amet
amet, consectetur adipiscing
lorem incididunt ut
lorem ipsum dolor sit amet amet, consectetur adipiscing lorem incididunt ut
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。

評論留言