我們一起玩轉 Grep 指令
在一個陽光明媚、晴空萬里的中午,一個撓頭的程序員正在與團隊一姐排查超時問題,只見一姐手速極快的查找著一個又一個日志,快速定位到一個又一個嫌疑人,仰慕之情油然而生,為了后續也能夠在小迷妹手上秀技術,所以暗下決心準備學習這個牛逼的東西。下面有請今天的主角(grep指令)閃亮登場。
一、基本語法
grep這個linux指令大家一定不陌生,其用于查找文件中符合條件的字符串,下面來看看這個高頻的指令如何使用。
- grep [選項] 查找內容 [源文件]
觀察其組成結構,由四部分組成:指令名(grep)、選項、查找內容、源文件,其中需要注意的有兩個位置,下面讓我們徐徐道來。
源文件
源文件部分是可有可無的,若不指定任何文件名稱或是所給予的文件名為-,則grep指令會從標準輸入設備讀取數據,其使用如下所示:
- // 文件路徑為/test
- // 接收cat的輸入
- cat ./test |grep 'hello'
- // 存在路徑部分參數
- grep 'hello' ./test
選項部分
選項部分比較多,可以通過grep --help指令來看一下有哪些選項:
- Regexp selection and interpretation: // 正則表達式選擇和解釋
- -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
看著選項內容真的很多,背起來著實不易,幸好文檔中給我們做了分類,只需要記住這些分類是干什么的,然后在需要的時候從里面進行搜索即可快速搜尋到所需用法(感覺看其內容必看菜鳥教程上的內容容易很多)
(1)當需要通過正則的方式進行搜索內容時,去"Regexp selection and interpretation"區塊找選項即可,常用的有:
- -E:通過正則表達式進行搜索
(2)當需要對輸出的內容進行控制時,去"Output control"區塊找選項即可,常用的有如下幾個:
- -m 數量:表征匹配多少次就會停止
- -n:顯示匹配行及行號
- -H:打印每一個匹配的文件名
- -r:能夠遞歸查詢,即可以輸入文件夾查詢
- -c:統計匹配到行的個數
(3)當需要獲取輸出內容的上下文進行操縱時,去"Context control"區塊找選項即可,常用的有如下幾個:
- -B 數量、-A 數量、-C 數量:分別表征獲取內容前、后、前后幾行
- --color:對輸出的內容添加顏色
(4)除了一些劃分比較理解的選項,還有一些選項我個人認為劃分的并不是很合理,但是它們仍然很重要,讓我們一起來看看有哪些:
- -i:忽略字母大小寫
- -v:反向選擇,也就是顯示出沒有搜索出字符串內容的那一行
二、經典用法
上面已經將其基本使用做了詳細的闡述,俗話說的好:光說不練假把式,光練不說真把式,連說帶練全把式。既然上面闡述了一通理論的東西,下面我們就來實戰幾個常用場景,將理論付諸于實踐。在實戰之前先創建一個文件,文件名是test,文件內容如下所示:
- hello world!!!
- dog
- cat
- pig
- big pig
- tiger
- Elephant
從確定文件中過濾出包含pig的
- $ grep 'pig' ./test
- pig
- big pig
從包含某一部分內容的文件中過濾包含pig的
- $ grep 'pig' ./te*
- pig
- big pig
從某一文件夾下所有內容中過濾出包含pig的
- $ grep -r 'pig' .
- ./test:pig
- ./test:big pig
從某一文件中過濾出不包含pig的
- $ grep -v 'pig' ./test
- hello world!!!
- dog
- cat
- tiger
- Elephant
在過濾文件時顯示行數
- $ grep -n 'pig' ./test
- 4:pig
- 5:big pig
匹配出以開頭的內容(通過基本正則表達式匹配即可,基本正則表達式字符有^$.[]*)
- $ grep ^p ./test
- pig
匹配出包含pig或cat內容的行(用到了擴展正則表達式,其在基本正則表達式基礎上增加了(){}?+|等)
- $ grep -E 'pig|cat' ./test
- cat
- pig
- big pig
匹配出包含hello和world內容的行
- $ grep 'hello' ./test |grep 'world'
- hello world!!!
獲取到匹配內容‘big pig'的前一行內容
- $ grep -B1 'big pig' ./test
- pig
- big pig
獲取匹配到'pig'行的數量
- $ grep -c 'pig' ./test
- 2
獲取到的pig行的內容高亮顯示
- $ grep --color 'pig' ./test
- pig
- big pig
經典用法還有很多,不能再一一進行羅列了,只需要知道在過濾內容時用此技巧能解決80%的問題,但這就足夠讓自己成為最亮的那個崽。
·大家好,我是執鳶者,畢業于華中科技大學,新時代農民工,現在是百度前端研發工程師,著有《前端百題斬》、數十篇學習思維導圖(go、React、Redux、Vue、Vuex、操作系統、Linux、設計模式、js、webpack、nginx)以及大量前端進階文章。
本文轉載自微信公眾號「前端點線面」,可以通過以下二維碼關注。轉載本文請聯系前端點線面公眾號。