Ubuntu grep 命令详解


grep 可搜索各 FILE 中的 PATTERN 及标准输出。

默认情况下,PATTERN 是 BRE 基本正则表达式 (basic regular expression)。


譬如: grep -i 'hello world' menu.h main.c

 

基本语法


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

 

PATTERN 参数

# 参数 EN 解释 中文翻译 备注
01 -E, --extended-regexp PATTERN is an extended regular expression (ERE)
02 -F, --fixed-strings PATTERN is a set of newline-separated fixed strings
03 -G, --basic-regexp PATTERN is a basic regular expression (BRE)
04 -P, --perl-regexp PATTERN is a Perl regular expression
05 -e, --regexp=PATTERN use PATTERN for matching
06 -f, --file=FILE obtain PATTERN from FILE
07 -i, --ignore-case ignore case distinctions
08 -w, --word-regexp force PATTERN to match only whole words
09 -x, --line-regexp force PATTERN to match only whole lines
10 -z, --null-data a data line ends in 0 byte, not newline

 

Miscellaneous 杂项

# 参数 EN 解释 中文翻译 备注
01 -s, --no-messages suppress error messages
02 -v, --invert-match select non-matching lines
03 -V, --version print version information and exit
04 --help display this help and exit 显示帮助并退出
05 --mmap deprecated no-op; evokes a warning

 

Output control 输出控制

# 参数 EN 解释 中文翻译 备注
01 -m, --max-count=NUM stop after NUM matches
02 -b, --byte-offset print the byte offset with output lines
03 -n, --line-number print line number with output lines
04 --line-buffered flush output on every line
05 -H, --with-filename print the file name for each match
06 -h, --no-filename suppress the file name prefix on output
07 --label=LABEL use LABEL as the standard input file name prefix
08 -o, --only-matching show only the part of a line matching PATTERN
09 -q, --quiet, --silent suppress all normal output
10 --binary-files=TYPE assume that binary files are TYPE; TYPE is 'binary', 'text', or 'without-match'
11 -a, --text equivalent to --binary-files=text
12 -I equivalent to --binary-files=without-match
13 -d, --directories=ACTION how to handle directories; ACTION is 'read', 'recurse', or 'skip'
14 -D, --devices=ACTION how to handle devices, FIFOs and sockets; ACTION is 'read' or 'skip'
15 -r, --recursive like --directories=recurse
16 -R, --dereference-recursive likewise, but follow all symlinks
17 --include=FILE_PATTERN search only files that match FILE_PATTERN
18 --exclude=FILE_PATTERN skip files and directories matching FILE_PATTERN
19 --exclude-from=FILE skip files matching any file pattern from FILE
20 --exclude-dir=PATTERN directories that match PATTERN will be skipped.
21 -L, --files-without-match print only names of FILEs containing no match
22 -l, --files-with-matches print only names of FILEs containing matches
23 -c, --count print only a count of matching lines per FILE
24 -T, --initial-tab make tabs line up (if needed)
25 -Z, --null print 0 byte after FILE name

 

Context control 上下文控制

# 参数 EN 解释 中文翻译 备注
01 -B, --before-context=NUM print NUM lines of leading context
02 -A, --after-context=NUM print NUM lines of trailing context
03 -C, --context=NUM print NUM lines of output context
04 -NUM same as --context=NUM
05 --color[=WHEN], --colour[=WHEN] use markers to highlight the matching strings; WHEN is 'always', 'never', or 'auto'
06 -U, --binary do not strip CR characters at EOL (MSDOS/Windows)
07 -u, --unix-byte-offsets report offsets as if CRs were not there (MSDOS/Windows)
数字 Python IDE

数字 Python IDE

Windows Vista / 7 / 8 / 10 (x64 位)

下载     网盘 1     网盘 2     网盘 3 (提取码 cs7s)

 

数字网页 IDE

数字网页 IDE

Windows Vista / 7 / 8 / 10 (x64 位)

下载     网盘 1     网盘 2     网盘 3 (提取码 cs7s)

 

数字管家

数字管家

Windows Vista / 7 / 8 / 10 (x64 位)

下载     网盘 1     网盘 2     网盘 3

 

数字翻译

数字翻译

Windows Vista / 7 / 8 / 10 (x64 位)

下载     网盘 1     网盘 2     网盘 3

详细用法

功能 # 命令规范 文字解释 示例 示例解释