16 個 grep 命令示例,可在現實世界中為您提供幫助
已發表: 2021-02-23grep 最初是為基於 Unix 的系統開發的,是 Linux 機器中使用最廣泛的命令行實用程序之一。
它的名字來源於ed工具中另一個類似的命令,即g/ re /p,代表g lobally search for a regular expression and p rint matching lines。 grep 基本上從標準輸入或文件中搜索給定的模式或正則表達式,並打印符合給定條件的行。 它通常用於過濾掉不必要的細節,同時從大日誌文件中僅打印所需的信息。
正則表達式的強大功能與 grep 中支持的選項相結合,使這成為可能。
在這裡,我們將介紹一些系統管理員或開發人員在不同場景下常用的 grep 命令。
那麼讓我們開始吧……
grep 命令語法
如果在沒有管道的情況下使用,grep 命令需要一個模式和可選參數以及一個文件列表。
$ grep [options] pattern [files]
一個簡單的例子是:
$ grep my file.txt my_file $
搜索多個文件
grep 使您不僅可以在一個文件中而且可以在多個文件中搜索給定的模式。 下面介紹如何使用*
通配符在多個文件中查找模式。
$ sudo grep -i err /var/log/messages*
輸出:
$ sudo grep err /var/log/messages* /var/log/messages:Dec 28 10:36:52 centos7vm kernel: ACPI: Using IOAPIC for interrupt routing /var/log/messages:Dec 28 10:36:52 centos7vm kernel: ACPI: PCI Interrupt Link [LNKA] (IRQs 5 9 10 *11) /var/log/messages:Dec 28 10:36:52 centos7vm kernel: ACPI: PCI Interrupt Link [LNKB] (IRQs 5 9 *10 11) /var/log/messages:Dec 28 10:36:52 centos7vm kernel: ACPI: PCI Interrupt Link [LNKC] (IRQs 5 *9 10 11) /var/log/messages:Dec 28 10:36:52 centos7vm kernel: ACPI: PCI Interrupt Link [LNKD] (IRQs 5 9 10 *11) /var/log/messages-20201225:Dec 23 23:01:00 centos7vm kernel: ACPI: Using IOAPIC for interrupt routing /var/log/messages-20201225:Dec 23 23:01:00 centos7vm kernel: ACPI: PCI Interrupt Link [LNKA] (IRQs 5 9 10 *11) /var/log/messages-20201225:Dec 23 23:01:00 centos7vm kernel: ACPI: PCI Interrupt Link [LNKB] (IRQs 5 9 *10 11) /var/log/messages-20201225:Dec 23 23:01:00 centos7vm kernel: ACPI: PCI Interrupt Link [LNKC] (IRQs 5 *9 10 11) /var/log/messages-20201225:Dec 23 23:01:00 centos7vm kernel: ACPI: PCI Interrupt Link [LNKD] (IRQs 5 9 10 *11) /var/log/messages-20201225:Dec 23 23:01:00 centos7vm kernel: BERT: Boot Error Record Table support is disabled. Enable it by using bert_enable as kernel parameter. /var/log/messages-20201227:Dec 27 19:11:18 centos7vm kernel: ACPI: PCI Interrupt Link [LNKA] (IRQs 5 9 10 *11) /var/log/messages-20201227:Dec 27 19:11:18 centos7vm kernel: ACPI: PCI Interrupt Link [LNKB] (IRQs 5 9 *10 11) /var/log/messages-20201227:Dec 27 19:11:18 centos7vm kernel: ACPI: PCI Interrupt Link [LNKC] (IRQs 5 *9 10 11) /var/log/messages-20201227:Dec 27 19:11:18 centos7vm kernel: ACPI: PCI Interrupt Link [LNKD] (IRQs 5 9 10 *11) /var/log/messages-20201227:Dec 27 19:11:18 centos7vm kernel: BERT: Boot Error Record Table support is disabled. Enable it by using bert_enable as kernel parameter. /var/log/messages-20201227:Dec 27 19:11:21 centos7vm kernel: [drm:vmw_host_log [vmwgfx]] *ERROR* Failed to send host log message. /var/log/messages-20201227:Dec 27 19:11:21 centos7vm kernel: [drm:vmw_host_log [vmwgfx]] *ERROR* Failed to send host log message. $
您可以從上面的輸出中觀察到,在打印匹配行之前首先打印了文件名,以指示grep
在哪裡找到了給定的模式。
不區分大小寫的搜索
grep 提供在不查看模式大小寫的情況下搜索模式。 使用-i
標誌告訴 grep 忽略大小寫。
$ grep -i [pattern] [file]
輸出:
$ grep -i it text_file.txt This is a sample text file. It contains functionality. You can always use grep with any kind of data but it works best with text data. It supports numbers like 1, 2, 3 etc. as well as This is a sample text file. It's repeated two times. $
全詞搜索
我們並不總是想要部分匹配,而是希望 grep 只匹配一個完整的單詞。 您可以使用-w
標誌來做到這一點。
$ grep -w [pattern] [file]
輸出:
$ grep -w is text_file.txt This is a sample text file. It contains This is a sample text file. It's repeated two times. $
檢查匹配計數
有時我們需要的不是實際匹配的行,而是 grep 進行的成功匹配的計數。 我們可以使用-c
選項獲取此計數。
$ grep -c [pattern] [file]
輸出:
$ grep -c is text_file.txt 2 $
搜索子目錄
通常不僅需要在當前工作目錄中搜索文件,還需要在子目錄中搜索文件。 grep 允許您使用-r
標誌輕鬆地做到這一點。
$ grep -r [pattern] *
輸出:
$ grep -r Hello * dir1/file1.txt:Hello One dir1/file2.txt:Hello Two dir1/file3.txt:Hello Three $
如您所見,grep 遍歷當前目錄中的每個子目錄,並列出找到匹配項的文件和行。
逆向搜索
如果你想找到與給定模式不匹配的東西,grep 允許使用-v
標誌來做到這一點。
$ grep -v [pattern] [file]
輸出:
$ grep This text_file.txt This is a sample text file. It contains This is a sample text file. It's repeated two times. $ grep -v This text_file.txt several lines to be used as part of testing grep functionality. You can always use grep with any kind of data but it works best with text data. It supports numbers like 1, 2, 3 etc. as well as alphabets and special characters like - + * # etc. $
您可以比較帶有和不帶有-v
標誌的相同模式和文件的grep
命令的輸出。 使用-v
,將打印與模式不匹配的任何行。
打印行號
grep 允許您打印行號以及打印的行,這使得您很容易知道該行在文件中的位置。 如圖所示使用-n
選項獲取輸出中的行號。
$ grep -n [pattern] [file]
輸出:
$ grep -n This text_file.txt 1:This is a sample text file. It contains 7:This is a sample text file. It's repeated two times. $
限制 grep 輸出
對於日誌等大文件,grep 輸出可能很長,您可能只需要輸出中固定數量的行,而不是匹配所有內容。 我們可以使用m[num]
來限制打印的行數。 下面是如何使用它:
$ grep -m[num] [pattern] [file]
請注意-m
標誌的使用如何影響 grep 在以下示例中針對同一組條件的輸出:
$ grep It text_file.txt This is a sample text file. It contains It supports numbers like 1, 2, 3 etc. as well as This is a sample text file. It's repeated two times. $ grep -m2 It text_file.txt This is a sample text file. It contains It supports numbers like 1, 2, 3 etc. as well as $
顯示附加行
通常我們不僅需要具有匹配模式的行,還需要在其上方或下方的一些行以獲得更好的上下文。

通過使用帶有num
值的-A
、 -B
或-C
標誌,可以使用 grep 在具有模式的行上方或下方(或兩者)打印一行。 這裡的num
表示要打印的額外行數,正好在匹配行的上方或下方。 這適用於 grep 在指定文件或文件列表中找到的所有匹配項。
$ grep -A[num] [pattern] [file]
或者
$ grep -B[num] [pattern] [file]
或者
$ grep -C[num] [pattern] [file]
下面的輸出顯示了正常的 grep 輸出以及帶有標誌-A
、 -B
和-C
的輸出。 請注意 grep 如何解釋標誌及其值以及相應輸出的變化。 使用-A1
標誌,grep 打印緊跟在匹配行之後的 1 行。
同樣,使用-B1
標誌,它會在匹配行之前打印 1 行。 使用-C1
標誌,它打印匹配行前後的 1 行。
$ grep numbers text_file.txt It supports numbers like 1, 2, 3 etc. as well as $ grep -A1 numbers text_file.txt It supports numbers like 1, 2, 3 etc. as well as alphabets and special characters like - + * # etc. $ grep -B1 numbers text_file.txt kind of data but it works best with text data. It supports numbers like 1, 2, 3 etc. as well as $ grep -C1 numbers text_file.txt kind of data but it works best with text data. It supports numbers like 1, 2, 3 etc. as well as alphabets and special characters like - + * # etc. $
列出文件名
要僅打印找到模式的文件的名稱而不是實際匹配的行,請使用-l
標誌。
$ grep -l [pattern] [file]
這是一個示例運行:
$ grep -l su *.txt file.txt text_file.txt $
打印精確的線條
有時我們需要打印與給定模式完全匹配的行,而不是它的某些部分。 grep 允許-x
標誌來做到這一點。
$ grep -x [pattern] [file]
在下面的示例中,file.txt 包含只有一個單詞“support”的行,因此被帶有-x
標誌的 grep 匹配,同時忽略可能包含單詞“support”和其他文本的行。
$ grep -x support *.txt file.txt:support $
匹配起始字符串
使用正則表達式,我們可以在一行的開頭找到一個字符串。 這是如何做的。
$ grep [options] "^[string]" [file]
例子:
$ grep It text_file.txt This is a sample text file. It contains It supports numbers like 1, 2, 3 etc. as well as This is a sample text file. It's repeated two times. $ grep ^It text_file.txt It supports numbers like 1, 2, 3 etc. as well as $
觀察使用^
字符如何改變輸出。 ^
表示字符串的開頭,grep 將^It
匹配為以單詞It
開頭的任何行。 當模式包含空格等時,用引號括起來會有所幫助。
匹配結束字符串
另一個常用的有用的正則表達式是匹配行模式的結尾。
$ grep [options] "[string]$" [file]
例子:
$ grep "\." text_file.txt This is a sample text file. It contains functionality. You can always use grep with any kind of data but it works best with text data. It supports numbers like 1, 2, 3 etc. as well as alphabets and special characters like - + * # etc. This is a sample text file. It's repeated two times. $ grep "\.$" text_file.txt kind of data but it works best with text data. alphabets and special characters like - + * # etc. This is a sample text file. It's repeated two times. $
我們試圖匹配一個.
行尾的字符。 由於點(.)是一個特殊含義的字符,我們需要用\
字符對其進行轉義。 再次注意當我們只匹配時輸出是如何變化的.
字符,當我們使用$
指示 grep 僅匹配以.
(不是那些可能在兩者之間的任何地方包含它的)。
使用花樣文件
在某些情況下,您可能有一些經常使用的複雜模式列表。 您可以在文件中指定模式列表並使用-f
標誌,而不是每次都寫下來。 該文件每行應包含一個模式。
$ grep -f [pattern_file] [file_to_match]
在我們的示例中,我們創建了具有以下內容的模式文件名pattern.txt
:
$ cat pattern.txt This It $
要使用它,請使用-f
標誌。
$ grep -f pattern.txt text_file.txt This is a sample text file. It contains It supports numbers like 1, 2, 3 etc. as well as This is a sample text file. It's repeated two times. $
指定多個模式
grep 允許使用-e
標誌指定多個模式。
$ grep -e [pattern1] -e [pattern2] -e [pattern3]...[file]
例子:
$ grep -e is -e It -e to text_file.txt This is a sample text file. It contains several lines to be used as part of testing grep It supports numbers like 1, 2, 3 etc. as well as This is a sample text file. It's repeated two times. $
指定擴展正則表達式
grep 還使用-E
標誌支持擴展正則表達式或 ERE。 這類似於 Linux 中的egrep
命令。
當您想按原樣處理元字符並且不想像 grep 那樣將它們替換為字符串時,使用 ERE 有一個優勢。 這在轉義它們方面為您提供了更大的靈活性,就像我們在 grep 的情況下所要求的那樣。 也就是說,將-E
與 grep 一起使用等同於egrep
命令。
$ grep -E '[Extended RegEx]' [file]
這是 ERE 的一種用法,我們想要打印未註釋或空白的行。 這對於在大配置文件中查找內容特別有用。 我還使用了-v
標誌來不打印與模式'^(#|$)'
匹配的行。
$ sudo grep -vE '^(#|$)' /etc/ssh/sshd_config HostKey /etc/ssh/ssh_host_rsa_key HostKey /etc/ssh/ssh_host_ecdsa_key HostKey /etc/ssh/ssh_host_ed25519_key SyslogFacility AUTHPRIV AuthorizedKeysFile .ssh/authorized_keys PasswordAuthentication yes ChallengeResponseAuthentication no GSSAPIAuthentication yes GSSAPICleanupCredentials no UsePAM yes X11Forwarding yes AcceptEnv LANG LC_CTYPE LC_NUMERIC LC_TIME LC_COLLATE LC_MONETARY LC_MESSAGES AcceptEnv LC_PAPER LC_NAME LC_ADDRESS LC_TELEPHONE LC_MEASUREMENT AcceptEnv LC_IDENTIFICATION LC_ALL LANGUAGE AcceptEnv XMODIFIERS Subsystem sftp /usr/libexec/openssh/sftp-server $
結論
以上例子只是冰山一角。 grep 支持一系列選項,對於知道如何有效使用它的人來說,它可能是一個非常有用的工具。 我們不僅可以使用上面給出的例子,還可以以不同的方式組合它們來獲得我們需要的東西。
請參閱其手冊頁以了解更多信息。
$ man grep
接下來,學習 SFTP 命令示例。