linux中g(shù)rep命令使用實(shí)戰(zhàn)詳解
一. grep命令介紹
Linux系統(tǒng)中g(shù)rep命令是一種強(qiáng)大的文本搜索工具,它能使用正則表達(dá)式搜索文本,并把匹 配的行打印出來(lái)。
grep全稱(chēng)是Global Regular Expression Print,表示全局正則表達(dá)式版本,它的使用權(quán)限是所有用戶(hù)。
英文注解:
grep ['grep] 搜索目標(biāo)行命令· global [?glo?bl] 全球的,球狀的 regular 美 [?r?ɡj?l?] 有規(guī)律的,規(guī)則的, 正規(guī)軍(n) expression 美 [?k?spr???n] 表達(dá),表現(xiàn),表情,臉色,態(tài)度
例句: It's enough to make you wet yourself, if you'll pardon the expression
linux支持三種形式的grep命令: grep , egrep ,grep -E
二. 語(yǔ)法格式及常用選項(xiàng)
依據(jù)慣例,我們還是先查看幫助,使用grep --help
[root@mufeng test]# grep --help 用法: grep [選項(xiàng)]... PATTERN [FILE]... 在每個(gè) FILE 或是標(biāo)準(zhǔn)輸入中查找 PATTERN。 默認(rèn)的 PATTERN 是一個(gè)基本正則表達(dá)式(縮寫(xiě)為 BRE)。 例如: grep -i 'hello world' menu.h main.c 正則表達(dá)式選擇與解釋: -E, --extended-regexp PATTERN 是一個(gè)可擴(kuò)展的正則表達(dá)式(縮寫(xiě)為 ERE) -F, --fixed-strings PATTERN 是一組由斷行符分隔的定長(zhǎng)字符串。 -G, --basic-regexp PATTERN 是一個(gè)基本正則表達(dá)式(縮寫(xiě)為 BRE) -P, --perl-regexp PATTERN 是一個(gè) Perl 正則表達(dá)式 -e, --regexp=PATTERN 用 PATTERN 來(lái)進(jìn)行匹配操作 -f, --file=FILE 從 FILE 中取得 PATTERN -i, --ignore-case 忽略大小寫(xiě) -w, --word-regexp 強(qiáng)制 PATTERN 僅完全匹配字詞 -x, --line-regexp 強(qiáng)制 PATTERN 僅完全匹配一行 -z, --null-data 一個(gè) 0 字節(jié)的數(shù)據(jù)行,但不是空行 Miscellaneous: -s, --no-messages suppress error messages -v, --invert-match select non-matching lines -V, --version display version information and exit --help display this help text and exit 輸出控制: -m, --max-count=NUM NUM 次匹配后停止 -b, --byte-offset 輸出的同時(shí)打印字節(jié)偏移 -n, --line-number 輸出的同時(shí)打印行號(hào) --line-buffered 每行輸出清空 -H, --with-filename 為每一匹配項(xiàng)打印文件名 -h, --no-filename 輸出時(shí)不顯示文件名前綴 --label=LABEL 將LABEL 作為標(biāo)準(zhǔn)輸入文件名前綴 -o, --only-matching show only the part of a line matching PATTERN -q, --quiet, --silent suppress all normal output --binary-files=TYPE assume that binary files are TYPE; TYPE is 'binary', 'text', or 'without-match' -a, --text equivalent to --binary-files=text -I equivalent to --binary-files=without-match -d, --directories=ACTION how to handle directories; ACTION is 'read', 'recurse', or 'skip' -D, --devices=ACTION how to handle devices, FIFOs and sockets; ACTION is 'read' or 'skip' -r, --recursive like --directories=recurse -R, --dereference-recursive likewise, but follow all symlinks --include=FILE_PATTERN search only files that match FILE_PATTERN --exclude=FILE_PATTERN skip files and directories matching FILE_PATTERN --exclude-from=FILE skip files matching any file pattern from FILE --exclude-dir=PATTERN directories that match PATTERN will be skipped. -L, --files-without-match print only names of FILEs containing no match -l, --files-with-matches print only names of FILEs containing matches -c, --count print only a count of matching lines per FILE -T, --initial-tab make tabs line up (if needed) -Z, --null print 0 byte after FILE name 文件控制: -B, --before-context=NUM 打印以文本起始的NUM 行 -A, --after-context=NUM 打印以文本結(jié)尾的NUM 行 -C, --context=NUM 打印輸出文本NUM 行 -NUM same as --context=NUM --group-separator=SEP use SEP as a group separator --no-group-separator use empty string as a group separator --color[=WHEN], --colour[=WHEN] use markers to highlight the matching strings; WHEN is 'always', 'never', or 'auto' -U, --binary do not strip CR characters at EOL (MSDOS/Windows) -u, --unix-byte-offsets report offsets as if CRs were not there (MSDOS/Windows)
為了更直觀一些,我們把常用的參數(shù)用表格來(lái)展示:
參數(shù) | 描述 |
---|---|
-i | 忽略大小寫(xiě) |
-E | 啟用POSTIX擴(kuò)展正則表達(dá)式 |
-P | 啟用perl正則 |
-o | 只輸出正則表達(dá)式的匹配的內(nèi)容 |
-w | 整字匹配 |
-v | 取反,也就是不匹配的 |
-n | 輸出行號(hào) |
有了具體的參數(shù)之后,我們?cè)賮?lái)看實(shí)戰(zhàn)案例:
三. 參考案例
3.1 搜索文件中以root開(kāi)頭的文件
以root開(kāi)頭的文件,可以用 ^root 比如查看/etc/passwd 中以root開(kāi)頭的文件,操作如下:
[root@mufenggrow ~]# grep ^root /etc/passwd root:x:0:0:root:/root:/bin/bash
3.2 搜索文件中出現(xiàn)的root
搜某個(gè)單詞,我們直接在grep后面跟上單詞名字即可:
案例一: 搜索/etc/passwd中的root用戶(hù)
[root@mufenggrow ~]# grep "root" /etc/passwd root:x:0:0:root:/root:/bin/bash operator:x:11:0:operator:/root:/sbin/nologin [root@mufenggrow ~]#
案例二: 從多個(gè)文件中搜索root
root@mufenggrow ~]# echo root >> a.txt [root@mufenggrow ~]# echo root >> b.txt [root@mufenggrow ~]# grep "root" /etc/passwd a.txt b.txt /etc/passwd:root:x:0:0:root:/root:/bin/bash /etc/passwd:operator:x:11:0:operator:/root:/sbin/nologin a.txt:root b.txt:root [root@mufenggrow ~]#
3.3 搜索除了匹配行之外的行
此處使用-v 參數(shù),比如取反
案例一: 統(tǒng)計(jì)文件的行數(shù)且不包含空行
空行的表示方法: ^$
[root@mufenggrow ~]# cp /etc/passwd ./ ## 源文件一共35行 [root@mufenggrow ~]# cat /etc/passwd |wc -l 35 ## 追加空行進(jìn)去 [root@mufenggrow ~]# echo "" >> /etc/passwd [root@mufenggrow ~]# cat /etc/passwd |wc -l 36 ## 去掉空行測(cè)試 [root@mufenggrow ~]# grep -v ^$ /etc/passwd |wc -l 35 [root@mufenggrow ~]#
有時(shí)候我們修改了配置文件,文件中包含大量的# ,我們想去掉#查看內(nèi)容,就可以使用
[root@mufenggrow ~]# grep -v ^# passwd |wc -l 35
3.4 匹配的部分使用顏色顯示
這里可以使用 --color=auto,我們來(lái)查看一下包含root的行,并高亮顯示要查找的root。
[root@mufenggrow ~]# grep root /etc/passwd --color=auto root:x:0:0:root:/root:/bin/bash operator:x:11:0:operator:/root:/sbin/nologin [root@mufenggrow ~]#
這樣顯示,效果不明顯,我們看下圖:
可
以看到,所有的root都是紅色表示的。
3.5 只輸出文件中匹配到的地方
比如我們要查詢(xún)r(jià)oot,但我不想顯示包含root的行,而是只顯示要查詢(xún)的內(nèi)容:
此時(shí)需要使用 -o 參數(shù),代碼如下
[root@mufenggrow ~]# grep -o root /etc/passwd root root root root
要注意,如果一行中有10個(gè)root,這里就顯示10個(gè),而不是只顯示一個(gè),所以3.4的案例中我們查詢(xún)的時(shí)候,包含root的有兩行,但有4個(gè)root,在3.5案例中,顯示了所有的root。
3.6 輸出包含匹配字符串的行,并顯示所在的行數(shù)
此處可以使用-n 參數(shù), -n 會(huì)在一行的前面加上 行號(hào): 比如“4:”
我們來(lái)看下代碼示例:
[root@mufenggrow ~]# grep -n "root" passwd 1:root:x:0:0:root:/root:/bin/bash 11:operator:x:11:0:operator:/root:/sbin/nologin
我們要統(tǒng)計(jì)一個(gè)文件一共有多少行,也可以使用-n 參數(shù)
root@mufenggrow ~]# grep -n "" passwd |awk -F : '{print $1}' |tail -n 1 35
3.7 統(tǒng)計(jì)文件或者文本中包含匹配字符串的行數(shù)
此時(shí)可以用-c參數(shù):
[root@mufenggrow ~]# grep -c "root" passwd 2
包含root的有兩行, 如果我們要統(tǒng)計(jì)文本的行數(shù):
[root@mufenggrow ~]# grep -c "$" passwd 35
相當(dāng)于查找 $的行數(shù),可以看到一共有35個(gè)$符號(hào),也就是35行。
總結(jié)
grep命令在日常工作中,應(yīng)用的比較廣泛,一定要認(rèn)真學(xué)習(xí),記熟記牢常用參數(shù)。
到此這篇關(guān)于linux中g(shù)rep命令使用實(shí)戰(zhàn)詳解的文章就介紹到這了,更多相關(guān)linux grep命令內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
CentOS7安裝調(diào)試Mysql數(shù)據(jù)庫(kù)的步驟詳解【實(shí)例】
這篇文章主要介紹了CentOS7安裝調(diào)試Mysql數(shù)據(jù)庫(kù),本文分步驟通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-10-10CentOS 開(kāi)機(jī)啟動(dòng)自定義腳本詳解及實(shí)現(xiàn)
這篇文章主要介紹了CentOS 開(kāi)機(jī)啟動(dòng)自定義腳本的相關(guān)資料,有些時(shí)候我們需要在服務(wù)器里設(shè)置一個(gè)腳本,讓他一開(kāi)機(jī)就自己?jiǎn)?dòng),需要的朋友可以參考下2016-11-11CentOS6.5系統(tǒng)簡(jiǎn)單安裝與配置Nginx服務(wù)器的方法
這篇文章主要介紹了CentOS6.5系統(tǒng)簡(jiǎn)單安裝與配置Nginx服務(wù)器的方法,結(jié)合實(shí)例形式較為詳細(xì)的分析了CentOS6.5平臺(tái)安裝及配置nginx服務(wù)器的具體步驟、相關(guān)命令及使用方法,需要的朋友可以參考下2018-04-04linux系統(tǒng)下部署項(xiàng)目訪問(wèn)報(bào)404錯(cuò)誤的解決方法
這篇文章主要為大家詳細(xì)介紹了linux系統(tǒng)下部署項(xiàng)目訪問(wèn)報(bào)404錯(cuò)誤的解決方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-12-12linux服務(wù)器上安裝Anaconda與pytorch的詳細(xì)過(guò)程
這篇文章主要介紹了linux服務(wù)器上安裝Anaconda與pytorch的詳細(xì)過(guò)程,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-10-10Linux系統(tǒng)下Nginx支持ipv6配置的方法
這篇文章主要介紹了Linux系統(tǒng)下Nginx支持ipv6的方法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-12-12