linux中g(shù)rep命令使用實(shí)戰(zhàn)詳解
目錄
- 一. grep命令介紹
- 二. 語法格式及常用選項(xiàng)
- 三. 參考案例
- 3.1 搜索文件中以root開頭的文件
- 3.2 搜索文件中出現(xiàn)的root
- 3.3 搜索除了匹配行之外的行
- 3.4 匹配的部分使用顏色顯示
- 3.5 只輸出文件中匹配到的地方
- 3.6 輸出包含匹配字符串的行,并顯示所在的行數(shù)
- 3.7 統(tǒng)計(jì)文件或者文本中包含匹配字符串的行數(shù)
- 總結(jié)
一. grep命令介紹
Linux系統(tǒng)中g(shù)rep命令是一種強(qiáng)大的文本搜索工具,它能使用正則表達(dá)式搜索文本,并把匹 配的行打印出來。
grep全稱是Global Regular Expression Print,表示全局正則表達(dá)式版本,它的使用權(quán)限是所有用戶。
英文注解:
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
二. 語法格式及常用選項(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á)式(縮寫為 BRE)。例如: grep -i "hello world" menu.h main.c正則表達(dá)式選擇與解釋: -E, --extended-regexp PATTERN 是一個(gè)可擴(kuò)展的正則表達(dá)式(縮寫為 ERE) -F, --fixed-strings PATTERN 是一組由斷行符分隔的定長(zhǎng)字符串。 -G, --basic-regexpPATTERN 是一個(gè)基本正則表達(dá)式(縮寫為 BRE) -P, --perl-regexp PATTERN 是一個(gè) Perl 正則表達(dá)式 -e, --regexp=PATTERN 用 PATTERN 來進(jìn)行匹配操作 -f, --file=FILE 從 FILE 中取得 PATTERN -i, --ignore-case 忽略大小寫 -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-matchselect non-matching lines -V, --version display version information and exit --helpdisplay 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, --textequivalent to --binary-files=text -Iequivalent 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, --nullprint 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ù)用表格來展示:
有了具體的參數(shù)之后,我們?cè)賮砜磳?shí)戰(zhàn)案例:
三. 參考案例
3.1 搜索文件中以root開頭的文件
以root開頭的文件,可以用 ^root 比如查看/etc/passwd 中以root開頭的文件,操作如下:
[root@mufenggrow ~]# grep ^root /etc/passwdroot:x:0:0:root:/root:/bin/bash
3.2 搜索文件中出現(xiàn)的root
搜某個(gè)單詞,我們直接在grep后面跟上單詞名字即可:
案例一: 搜索/etc/passwd中的root用戶
[root@mufenggrow ~]# grep "root" /etc/passwdroot:x:0:0:root:/root:/bin/bashoperator: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/nologina.txt:rootb.txt:root[root@mufenggrow ~]#
3.3 搜索除了匹配行之外的行
此處使用-v 參數(shù),比如取反
案例一: 統(tǒng)計(jì)文件的行數(shù)且不包含空行
空行的表示方法: ^$
[root@mufenggrow ~]# cp /etc/passwd ./## 源文件一共35行[root@mufenggrow ~]# cat /etc/passwd |wc -l35## 追加空行進(jìn)去[root@mufenggrow ~]# echo "" >> /etc/passwd[root@mufenggrow ~]# cat /etc/passwd |wc -l36## 去掉空行測(cè)試[root@mufenggrow ~]# grep -v ^$ /etc/passwd |wc -l35[root@mufenggrow ~]#
有時(shí)候我們修改了配置文件,文件中包含大量的# ,我們想去掉#查看內(nèi)容,就可以使用
[root@mufenggrow ~]# grep -v ^# passwd |wc -l35
3.4 匹配的部分使用顏色顯示
這里可以使用 --color=auto,我們來查看一下包含root的行,并高亮顯示要查找的root。
[root@mufenggrow ~]# grep root /etc/passwd --color=autoroot:x:0:0:root:/root:/bin/bashoperator:x:11:0:operator:/root:/sbin/nologin[root@mufenggrow ~]#
這樣顯示,效果不明顯,我們看下圖:
可
以看到,所有的root都是紅色表示的。
3.5 只輸出文件中匹配到的地方
比如我們要查詢r(jià)oot,但我不想顯示包含root的行,而是只顯示要查詢的內(nèi)容:
此時(shí)需要使用 -o 參數(shù),代碼如下
[root@mufenggrow ~]# grep -o root /etc/passwdrootrootrootroot
要注意,如果一行中有10個(gè)root,這里就顯示10個(gè),而不是只顯示一個(gè),所以3.4的案例中我們查詢的時(shí)候,包含root的有兩行,但有4個(gè)root,在3.5案例中,顯示了所有的root。
3.6 輸出包含匹配字符串的行,并顯示所在的行數(shù)
此處可以使用-n 參數(shù), -n 會(huì)在一行的前面加上 行號(hào): 比如“4:”
我們來看下代碼示例:
[root@mufenggrow ~]# grep -n "root" passwd1:root:x:0:0:root:/root:/bin/bash11: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 135
3.7 統(tǒng)計(jì)文件或者文本中包含匹配字符串的行數(shù)
此時(shí)可以用-c參數(shù):
[root@mufenggrow ~]# grep -c "root" passwd2
包含root的有兩行, 如果我們要統(tǒng)計(jì)文本的行數(shù):
[root@mufenggrow ~]# grep -c "$" passwd35
相當(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)文章希望大家以后多多支持!
