Linux三劍客之grep

說(shuō)起Linux操作系統(tǒng)中的grep命令,或許沒(méi)有人會(huì)不知道。在我看來(lái),grep 命令是Linux操作系統(tǒng)上最強(qiáng)大的工具之一,正如你無(wú)時(shí)無(wú)刻不在使用它。無(wú)論是從文件中找到匹配的行,又或者是從終端輸出中獲取指定信息,都離不開(kāi)對(duì) grep 的使用。

了解一個(gè)工具的使用,還是先看這個(gè)工具的man手冊(cè)會(huì)告訴我們什么。

# man grep
GREP(1)                               General Commands Manual                              GREP(1)

NAME
       grep, egrep, fgrep - print lines matching a pattern

SYNOPSIS
       grep [OPTIONS] PATTERN [FILE...]
       grep [OPTIONS] [-e PATTERN | -f FILE] [FILE...]

DESCRIPTION
       grep  searches  the  named  input  FILEs  (or standard input if no files are named, or if a
       single hyphen-minus (-) is given as file name) for lines containing a match  to  the  given
       PATTERN.  By default, grep prints the matching lines.

       In  addition,  two  variant  programs  egrep and fgrep are available.  egrep is the same as
       grep -E.  fgrep is the same as grep -F.  Direct invocation as  either  egrep  or  fgrep  is
       deprecated,  but  is  provided  to  allow  historical applications that rely on them to run
       unmodified.
...

手冊(cè)中提到,grep 工具還有兩個(gè)孿生兄弟,分別是 egrepfgrep。其中,egrep 相當(dāng)于 grep -E 的用法,使用的是擴(kuò)展形式的正則表達(dá)式;fgrep 相當(dāng)于 grep -F 的用法,根據(jù)固定模式進(jìn)行內(nèi)容匹配。

參數(shù)詳解

對(duì)于任何的Linux操作系統(tǒng),你可以很輕松的找到/etc/passwd文件,以下的多數(shù)演示將會(huì)以該文件進(jìn)行演示。以下為用戶的部分信息

#cat /etc/passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin

grep 命令的格式相對(duì)比較簡(jiǎn)單。完整的命令格式如 grep [OPTIONS] PATTERN [FILE...],其中 [OPTIONS] 選項(xiàng)提供了眾多的參數(shù),如下

  • -E, --extended-regexp: 使用正則規(guī)則匹配相應(yīng)的內(nèi)容,相當(dāng)于egrep(一種并不推薦的用法)

如果需要找到包含root用戶和adm用戶的行,可以通過(guò)grep -E "root|adm" /etc/passwd來(lái)查找所有滿足的行

#grep -E "root|adm" /etc/passwd
root:x:0:0:root:/root:/bin/bash
adm:x:3:4:adm:/var/adm:/sbin/nologin
  • -F, --fixed-strings, --fixed-regexp: 將范本樣式看作固定的字符串列表
  • -e PATTERN, --regexp=PATTERN: 指定多個(gè)匹配模式。多個(gè)模式以 -e pattern1 -e pattern2 格式
# grep -e root -e adm /etc/passwd
root:x:0:0:root:/root:/bin/bash
adm:x:3:4:adm:/var/adm:/sbin/nologin
  • -f FILE, --file=FILE: 指定范本文件。范本文件中的格式為每行一個(gè)范本,根據(jù)范本文件中的內(nèi)容匹配內(nèi)容
# cat grep-file
adm
root
# grep -f grep-file /etc/passwd
root:x:0:0:root:/root:/bin/bash
adm:x:3:4:adm:/var/adm:/sbin/nologin
  • -i, --ignore-case: 搜索模式時(shí)忽略大小寫(xiě)
# cat grep-file
adm
root
LOGGER
Logging
# grep -i log grep-file
LOGGER
Logging
  • -v, --invert-match: 對(duì)搜索到的結(jié)果進(jìn)行取反,只輸出符合匹配模式以外的行
# grep -v root /etc/passwd
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
  • -w, --word-regexp: 以單詞作為匹配模式。例如,指定-w log將只能匹配到 log, 不能匹配到 logg、logging 等。
# grep -w Logg grep-file
# grep -w Logging grep-file
Logging
  • -x, --line-regexp: 以整行作為匹配模式。例如,指定-x log將只能匹配log,不能匹配到 logslog err 等。
# cat grep-file
adm
root
LOGGER
Logging
Logging record
# grep -x Logging grep-file
Logging
  • -c, --count: 返回匹配到的行數(shù),代替返回匹配到的內(nèi)容
# grep -c Log grep-file
3
  • -L, --files-without-match: 根據(jù)輸入文件匹配指定模式時(shí)沒(méi)有合適的匹配結(jié)果時(shí)輸出文件名稱,倘若在輸入文件中匹配到了內(nèi)容,不會(huì)輸出文件名
# grep -L root grep-file
# grep -L name grep-file
grep-file
  • -l, --files-with-matches: 根據(jù)輸入文件匹配指定模式時(shí)匹配到合適的結(jié)果時(shí)輸出文件名稱,與 -L 用法相反
# grep -l name grep-file
# grep -l root grep-file
grep-file
  • -m NUM, --max-count=NUM: 最多返回匹配到的記錄數(shù)
# grep -m 2 Log grep-file
Logging
Logging record
  • -o, --only-matching: 僅輸出匹配到的內(nèi)容。例如,用 roo 匹配 root 時(shí),將只匹配到 roo。通常和 -n 配合使用
# grep -o roo -n grep-file
2:roo    # 2 為行號(hào)
# grep -o root -n grep-file
2:root
  • -q, --quiet, --silent: 靜默模式,不會(huì)向標(biāo)準(zhǔn)輸出中寫(xiě)入任何東西(簡(jiǎn)單點(diǎn)說(shuō)就是終端不會(huì)返回任何內(nèi)容),一旦匹配到任何東西或出現(xiàn)錯(cuò)誤,將以 0 的返回值退出
# grep -o root -n -q grep-file
# echo $?
0
  • -s, --no-messages: 抑制出錯(cuò)信息。終端將不會(huì)打印標(biāo)準(zhǔn)錯(cuò)誤
# grep -s root  grep-
# grep  root  grep-
grep: grep-: 沒(méi)有那個(gè)文件或目錄
  • -b, --byte-offset: 輸出匹配到的內(nèi)容及開(kāi)始字符的偏移量。偏移量從文件頭部開(kāi)始,初始值為 0
# cat grep-file
adm
root
LOGGER
Logging
Logging record
Loggingrecord
# grep -b root grep-file
4:root
# grep -b ad grep-file
0:adm
  • -H, --with-filename: 匹配到內(nèi)容時(shí)將文件名稱與匹配到的內(nèi)容成對(duì)輸出。對(duì)于從多個(gè)文件中匹配內(nèi)容非常有幫助
# grep -H root /etc/passwd grep-file
/etc/passwd:root:x:0:0:root:/root:/bin/bash
/etc/passwd:operator:x:11:0:operator:/root:/sbin/nologin
grep-file:root
  • -n, --line-number: 匹配到結(jié)果時(shí)輸出對(duì)應(yīng)的行
# grep -n root grep-file
2:root
  • -A NUM, --after-context=NUM: 輸出匹配到的行及行后的多少行。
# grep -A 2 -n root grep-file
2:root
3-LOGGER
4-Logging
  • -B NUM, --before-context=NUM: 輸出匹配到的行及之前的多少行
# grep -B 2 -n root grep-file
1-adm
2:root
  • -C NUM, -NUM, --context=NUM: 輸出匹配到內(nèi)容的前后兩行
# grep -C 2 -n root grep-file
1-11111
2-adm
3:root
4-LOGGER
5-Loggin
  • -r, --recursive: 在指定目錄下遞歸查找所有文件中滿足匹配條件的。指定目錄下存在符號(hào)鏈接文件的,僅當(dāng)指定符號(hào)鏈接時(shí)才會(huì)在符號(hào)鏈接對(duì)應(yīng)的目錄下查找
  • -R, --dereference-recursive: 與 -r 參數(shù)用法基本一致,區(qū)別在于當(dāng)指定目錄下存在符號(hào)鏈接文件時(shí),會(huì)主動(dòng)在符號(hào)鏈接文件對(duì)應(yīng)的目錄下查找 (參考自: grep -r 和 -R有什么區(qū)別)

正則部分

了解 grep 的常用參數(shù)可以在工作中解決絕大部份的問(wèn)題,但在一些情況下,需要對(duì)過(guò)濾出的內(nèi)容按照某種規(guī)則進(jìn)行精確匹配,還需要配合正則使用。以下介紹了幾種常用的正則語(yǔ)法

  • ^ pattern: 匹配以pattern格式開(kāi)頭的行
# grep -E "^a" grep-file
adm
  • pattern $: 匹配以pattern模式結(jié)尾的行
# grep -E "record$" grep-file
Logging record
Loggingrecord
  • pattern +: 匹配指定模式一次或多次
# grep -E gr+ grep-file
Loggingrecord
  • pattern *: 匹配pattern模式結(jié)尾的所有內(nèi)容
# grep -E ad* grep-file
adm
  • pattern {n}: 匹配pattern模式n次
# grep -E g{2} grep-file  # 匹配至少包含2個(gè)g的單詞
Logging
Logging record
Loggingrecord
  • pattern {n,}: 匹配pattern模式n次或更多次
# grep -E o{1} grep-file
root
Logging
Logging record
Loggingrecord
# grep -E "o{2,}" grep-file
root
  • pattern {,m}: 匹配pattern模式最多m次
# cat grep-file
adm
root
LOGGER
Logging
Logggggggggggg
Logging record
Loggingrecord
# grep -E "g{,3}i" grep-file
Logging
Logging record
Loggingrecord
  • pattern {n,m}: 匹配pattern模式最少n次,最多m次
# grep -E "gg{1,3}i" grep-file
Logging
Logging record
Loggingrecord
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容