grep 2.25 命令詳解


在每個 FILE (或標準輸入) 中搜索 PATTERN。

默認情況下,PATTERN 是 BRE (基本正則錶達式)。

另請參閱: find     grep 主頁     killall     ps

基本語法     基本用法     詳細用法

grep [OPTION]... PATTERN [FILE]...
			

退齣狀態為 0,若選擇瞭任何行,否則為 1。 若發生任何錯誤且未給齣 -q,退齣狀態為 2。

當 FILE 為 - 時,讀取標準輸入。 沒有 FILE 讀取 .,若命令行給齣 -r;否則 -。 若給齣 FILE 少於 2,則假定 -h。

 

注意:

egrep 意味著 grep -E。

fgrep 意味著 grep -F。

直接援引 egrep 或 fgrep 已棄用。

Regexp selection and interpretation 正則錶達式的選擇和解釋

參數 EN 解釋 中文翻譯 備注
-E, --extended-regexp PATTERN is an extended regular expression (ERE) PATTERN 是 ERE (擴展正則錶達式)
-F, --fixed-strings PATTERN is a set of newline-separated strings 模式是換行符分隔的字符串集
-G, --basic-regexp PATTERN is a basic regular expression (BRE) PATTERN 是 BRE (基本正則錶達式)
-P, --perl-regexp PATTERN is a Perl regular expression PATTERN 是 Perl 正則錶達式
-e, --regexp=PATTERN use PATTERN for matching 使用 PATTERN 進行匹配
-f, --file=FILE obtain PATTERN from FILE 從 FILE 獲得 PATTERN
-i, --ignore-case ignore case distinctions 忽略大小寫的區彆
-w, --word-regexp force PATTERN to match only whole words 強製 PATTERN 隻匹配整個單詞
-x, --line-regexp force PATTERN to match only whole lines 強製 PATTERN 隻匹配整行
-z, --null-data a data line ends in 0 byte, not newline 數據行以 0 字節結尾,而不是換行符

 

Miscellaneous 雜項

參數 EN 解釋 中文翻譯 備注
-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 輸齣控製

參數 EN 解釋 中文翻譯 備注
-m, --max-count=NUM stop after NUM matches 在 NUM 個匹配後停止
-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 把 LABEL 用作標準輸入文件名的前綴
-o, --only-matching show only the part of a line matching PATTERN 僅展示匹配 PATTERN 行部分
-q, --quiet, --silent suppress all normal output 抑製所有正常輸齣
--binary-files=TYPE

assume that binary files are TYPE;

TYPE is 'binary', 'text', or 'without-match'

假定二進製文件是 TYPE;

TYPE 為 binary、text 或 without-match

-a, --text equivalent to --binary-files=text 等效於 --binary-files=text
-I equivalent to --binary-files=without-match 等效於 --binary-files=without-match
-d, --directories=ACTION

how to handle directories;

ACTION is 'read', 'recurse', or 'skip'

如何處理目錄;

ACTION 為 read、recurse 或 skip

-D, --devices=ACTION

how to handle devices, FIFOs and sockets;

ACTION is 'read' or 'skip'

如何處理設備、FIFO 和套接字;

ACTION 為 read 或 skip

-r, --recursive like --directories=recurse 類似 --directories=recurse
-R, --dereference-recursive likewise, but follow all symlinks 同上,但遵循所有符號鏈接
--include=FILE_PATTERN search only files that match FILE_PATTERN 僅搜索匹配 FILE_PATTERN 的文件
--exclude=FILE_PATTERN skip files and directories matching FILE_PATTERN 跳過匹配 FILE_PATTERN 的文件和目錄
--exclude-from=FILE skip files matching any file pattern from FILE 跳過與 FILE 中任何文件模式匹配的文件
--exclude-dir=PATTERN directories that match PATTERN will be skipped. 匹配 PATTERN 的目錄將被跳過。
-L, --files-without-match print only names of FILEs containing no match 隻打印包含不匹配的 FILE 的名稱
-l, --files-with-matches print only names of FILEs containing matches 隻打印包含匹配的 FILE 的名稱
-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 打印文件名後 0 字節

 

Context control 上下文控製

參數 EN 解釋 中文翻譯 備注
-B, --before-context=NUM print NUM lines of leading context 打印前導上下文的 NUM 行
-A, --after-context=NUM print NUM lines of trailing context 打印結尾上下文的 NUM 行
-C, --context=NUM print NUM lines of output context 打印輸齣上下文的 NUM 行
-NUM same as --context=NUM 如同 --context=NUM

--color[=WHEN]

--colour[=WHEN]

use markers to highlight the matching strings;

WHEN is 'always', 'never', or 'auto'

使用標記突顯匹配字符串;

WHEN 為 always、never 或 auto

-U, --binary do not strip CR characters at EOL 不要剝離 EOL CR 字符 (MSDOS/Windows)
-u, --unix-byte-offsets report offsets as if CRs were not there (MSDOS/Windows) 報告偏移,好像沒有 CR (MSDOS/Windows)

基本用法

功能 命令 文字解釋 示例 示例解釋
幫助 grep --help 展示幫助信息
版本 grep -V 展示版本信息
grep --version

詳細用法

按指定字符搜索指定文件,帶行號展示結果行

grep -n "404" /var/log/nginx/*.log
			

按指定字符搜索指定文件,忽略大小寫展示結果行

grep -i "timeout" /var/log/*.log
 	
grep -i "hello world" menu.h main.c
			

按指定字符搜索指定文件,展示結果行及後 3 行

grep -A 3 "404" /var/log/nginx/*.log
			
curl -I -m 5 -s -w "%{http_code}\n" -o /dev/null www.baidu.com | egrep "30[12]|200" | wc -l
			

 

版權聲明: 本文為獨傢原創稿件,版權歸 樂數軟件 ,未經許可不得轉載。