我們一起玩轉(zhuǎn) Grep 指令
在一個陽光明媚、晴空萬里的中午,一個撓頭的程序員正在與團(tuán)隊一姐排查超時問題,只見一姐手速極快的查找著一個又一個日志,快速定位到一個又一個嫌疑人,仰慕之情油然而生,為了后續(xù)也能夠在小迷妹手上秀技術(shù),所以暗下決心準(zhǔn)備學(xué)習(xí)這個牛逼的東西。下面有請今天的主角(grep指令)閃亮登場。
一、基本語法
grep這個linux指令大家一定不陌生,其用于查找文件中符合條件的字符串,下面來看看這個高頻的指令如何使用。
- grep [選項] 查找內(nèi)容 [源文件]
觀察其組成結(jié)構(gòu),由四部分組成:指令名(grep)、選項、查找內(nèi)容、源文件,其中需要注意的有兩個位置,下面讓我們徐徐道來。
源文件
源文件部分是可有可無的,若不指定任何文件名稱或是所給予的文件名為-,則grep指令會從標(biāo)準(zhǔn)輸入設(shè)備讀取數(shù)據(jù),其使用如下所示:
- // 文件路徑為/test
- // 接收cat的輸入
- cat ./test |grep 'hello'
- // 存在路徑部分參數(shù)
- grep 'hello' ./test
選項部分
選項部分比較多,可以通過grep --help指令來看一下有哪些選項:
- Regexp selection and interpretation: // 正則表達(dá)式選擇和解釋
- -E, --extended-regexp PATTERN is an extended regular expression (ERE)
- -F, --fixed-strings PATTERN is a set of newline-separated strings
- -G, --basic-regexp PATTERN is a basic regular expression (BRE)
- -P, --perl-regexp PATTERN is a Perl regular expression
- -e, --regexp=PATTERN use PATTERN for matching
- -f, --file=FILE obtain PATTERN from FILE
- -i, --ignore-case ignore case distinctions
- -w, --word-regexp force PATTERN to match only whole words
- -x, --line-regexp force PATTERN to match only whole lines
- -z, --null-data a data line ends in 0 byte, not newline
- 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
- Output control: // 輸出控制
- -m, --max-count=NUM stop after NUM matches
- -b, --byte-offset print the byte offset with output lines
- -n, --line-number print line number with output lines
- --line-buffered flush output on every line
- -H, --with-filename print the file name for each match
- -h, --no-filename suppress the file name prefix on output
- --label=LABEL use LABEL as the standard input file name prefix
- -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
- Context control: // 上下文控制
- -B, --before-context=NUM print NUM lines of leading context
- -A, --after-context=NUM print NUM lines of trailing context
- -C, --context=NUM print NUM lines of output context
- -NUM same as --context=NUM
- --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
看著選項內(nèi)容真的很多,背起來著實不易,幸好文檔中給我們做了分類,只需要記住這些分類是干什么的,然后在需要的時候從里面進(jìn)行搜索即可快速搜尋到所需用法(感覺看其內(nèi)容必看菜鳥教程上的內(nèi)容容易很多)
(1)當(dāng)需要通過正則的方式進(jìn)行搜索內(nèi)容時,去"Regexp selection and interpretation"區(qū)塊找選項即可,常用的有:
- -E:通過正則表達(dá)式進(jìn)行搜索
(2)當(dāng)需要對輸出的內(nèi)容進(jìn)行控制時,去"Output control"區(qū)塊找選項即可,常用的有如下幾個:
- -m 數(shù)量:表征匹配多少次就會停止
- -n:顯示匹配行及行號
- -H:打印每一個匹配的文件名
- -r:能夠遞歸查詢,即可以輸入文件夾查詢
- -c:統(tǒng)計匹配到行的個數(shù)
(3)當(dāng)需要獲取輸出內(nèi)容的上下文進(jìn)行操縱時,去"Context control"區(qū)塊找選項即可,常用的有如下幾個:
- -B 數(shù)量、-A 數(shù)量、-C 數(shù)量:分別表征獲取內(nèi)容前、后、前后幾行
- --color:對輸出的內(nèi)容添加顏色
(4)除了一些劃分比較理解的選項,還有一些選項我個人認(rèn)為劃分的并不是很合理,但是它們?nèi)匀缓苤匾?,讓我們一起來看看有哪些?/p>
- -i:忽略字母大小寫
- -v:反向選擇,也就是顯示出沒有搜索出字符串內(nèi)容的那一行
二、經(jīng)典用法
上面已經(jīng)將其基本使用做了詳細(xì)的闡述,俗話說的好:光說不練假把式,光練不說真把式,連說帶練全把式。既然上面闡述了一通理論的東西,下面我們就來實戰(zhàn)幾個常用場景,將理論付諸于實踐。在實戰(zhàn)之前先創(chuàng)建一個文件,文件名是test,文件內(nèi)容如下所示:
- hello world!!!
- dog
- cat
- pig
- big pig
- tiger
- Elephant
從確定文件中過濾出包含pig的
- $ grep 'pig' ./test
- pig
- big pig
從包含某一部分內(nèi)容的文件中過濾包含pig的
- $ grep 'pig' ./te*
- pig
- big pig
從某一文件夾下所有內(nèi)容中過濾出包含pig的
- $ grep -r 'pig' .
- ./test:pig
- ./test:big pig
從某一文件中過濾出不包含pig的
- $ grep -v 'pig' ./test
- hello world!!!
- dog
- cat
- tiger
- Elephant
在過濾文件時顯示行數(shù)
- $ grep -n 'pig' ./test
- 4:pig
- 5:big pig
匹配出以開頭的內(nèi)容(通過基本正則表達(dá)式匹配即可,基本正則表達(dá)式字符有^$.[]*)
- $ grep ^p ./test
- pig
匹配出包含pig或cat內(nèi)容的行(用到了擴展正則表達(dá)式,其在基本正則表達(dá)式基礎(chǔ)上增加了(){}?+|等)
- $ grep -E 'pig|cat' ./test
- cat
- pig
- big pig
匹配出包含hello和world內(nèi)容的行
- $ grep 'hello' ./test |grep 'world'
- hello world!!!
獲取到匹配內(nèi)容‘big pig'的前一行內(nèi)容
- $ grep -B1 'big pig' ./test
- pig
- big pig
獲取匹配到'pig'行的數(shù)量
- $ grep -c 'pig' ./test
- 2
獲取到的pig行的內(nèi)容高亮顯示
- $ grep --color 'pig' ./test
- pig
- big pig
經(jīng)典用法還有很多,不能再一一進(jìn)行羅列了,只需要知道在過濾內(nèi)容時用此技巧能解決80%的問題,但這就足夠讓自己成為最亮的那個崽。
·大家好,我是執(zhí)鳶者,畢業(yè)于華中科技大學(xué),新時代農(nóng)民工,現(xiàn)在是百度前端研發(fā)工程師,著有《前端百題斬》、數(shù)十篇學(xué)習(xí)思維導(dǎo)圖(go、React、Redux、Vue、Vuex、操作系統(tǒng)、Linux、設(shè)計模式、js、webpack、nginx)以及大量前端進(jìn)階文章。
本文轉(zhuǎn)載自微信公眾號「前端點線面」,可以通過以下二維碼關(guān)注。轉(zhuǎn)載本文請聯(lián)系前端點線面公眾號。