Ubuntu grep 2.25 命令详解


在每个 FILE (或标准输入) 中搜索 PATTERN。

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

另请参阅: find     grep 主页     killall     ps

基本用法     详细用法

grep -i 'hello world' menu.h main.c
 
curl -I -m 5 -s -w "%{http_code}\n" -o /dev/null www.baidu.com | egrep "30[12]|200" | wc -l
			

基本语法

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

 

版权声明: 本文为独家原创稿件,版权归 乐数软件 ,未经许可不得转载。