【学习笔记】【linux】【grep命令】grep常用参数指南
程序员文章站
2024-02-23 23:26:52
...
【官方文档】
>> grep --help
Usage: grep [OPTION]... PATTERN [FILE] ...
Search for PATTERN in each FILE or standard input.
// 功能说明:查找文件或者输入内容中里符合条件的字符串。
Example: grep -i 'hello world' menu.h main.c
Regexp selection and interpretation:
-E, --extended-regexp PATTERN is an extended regular expression
//将范本样式为延伸的普通表示法来使用
-F, --fixed-strings PATTERN is a set of newline-separated strings
//范本样式视为固定字符串的列表
-G, --basic-regexp PATTERN is a basic regular expression
//将范本样式视为普通的表示法来使用
-P, --perl-regexp PATTERN is a Perl regular expression
-e, --regexp=PATTERN use PATTERN as a regular expression
//指定字符串做为查找文件内容的范本样式。
-f, --file=FILE obtain PATTERN from FILE
-i, --ignore-case ignore case distinctions
//不区分大小写地搜索。默认情况区分大小写
-w, --word-regexp force PATTERN to match only whole words
//只匹配整个单词,而不是字符串的一部分(如匹配‘magic’,而不是‘magical’)
-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 print version information and exit
//显示版本信息
--help display this help and exit
--mmap use memory-mapped input if possible
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 filename for each match
//在显示符合范本样式的那一列之前,表示该列所属的文件名称
-h, --no-filename suppress the prefixing filename on output
//在显示符合范本样式的那一列之前,不标示该列所属的文件名称
--label=LABEL print LABEL as filename for standard input
-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'
//当指定要查找的是目录而非文件时,必须使用这项参数,否则grep指令将回报信息并停止动作。
-D, --devices=ACTION how to handle devices, FIFOs and sockets
ACTION is 'read' or 'skip'
-R, -r, --recursive equivalent to --directories=recurse
--include=PATTERN files that match PATTERN will be examined
--exclude=PATTERN files that match PATTERN will be skipped.
--exclude-from=FILE files that match PATTERN in FILE will be skipped.
-L, --files-without-match only print FILE names containing no match
//列出不匹配的文件名
-l, --files-with-matches only print FILE names containing matches
//只列出匹配的文件名,列出文件内容符合指定的范本样式的文件名称
-c, --count only print a count of matching lines per FILE
//计算符合范本样式的列数
-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
//匹配的上下文分别显示[number]行,
//除了显示符合范本样式的那一列之外,并显示该列之前后的内容
-NUM same as --context=NUM
--color[=WHEN],
--colour[=WHEN] use markers to distinguish the matching string
WHEN may be `always', `never' or `auto'.
-U, --binary do not strip CR characters at EOL (MSDOS)
-u, --unix-byte-offsets report offsets as if CRs were not there (MSDOS)
`egrep' means `grep -E'. `fgrep' means `grep -F'.
With no FILE, or when FILE is -, read standard input. If less than
two FILEs given, assume -h. Exit status is 0 if match, 1 if no match,
and 2 if trouble.
下一篇: C#之七层登录
推荐阅读