grep版本:
# grep -V
grep (GNU grep) 2.20
...
grep --help(或者"man grep")查看详细语法及参数
语法:
grep [选项]... 模式[文件]...
# grep --help
Usage: grep [OPTION]... PATTERN [FILE]...
Search for PATTERN in each FILE or standard input.
#在每个文件或标准输入中搜索PATTERN。
PATTERN is, by default, a basic regular expression (BRE).
#PATTERN默认为基本正则表达式(basic regular expression, BRE)。
Example: grep -i 'hello world' menu.h main.c
#例如:grep -i 'hello world' menu.h main.c
Regexp selection and interpretation:
#正则表达式的选择和解释:
-E, --extended-regexp PATTERN is an extended regular expression (ERE)
#模式是一种扩展的常规表达方式
-F, --fixed-strings PATTERN is a set of newline-separated fixed strings
#模式将样式视为固定字符串的列表,是一组新行分隔的固定字符串
-G, --basic-regexp PATTERN is a basic regular expression (BRE)
#模式是一个基本的正则表达式(BRE)
-P, --perl-regexp PATTERN is a Perl regular expression
#模式是一个Perl正则表达式
-e, --regexp=PATTERN use PATTERN for matching
#使用PATTERN进行匹配
-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
--group-separator=SEP use SEP as a group separator
--no-group-separator use empty string as a group separator
--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
(MSDOS/Windows)
'egrep' means 'grep -E'. 'fgrep' means 'grep -F'.
Direct invocation as either 'egrep' or 'fgrep' is deprecated.
When FILE is -, read standard input. With no FILE, read . if a command-line
-r is given, - otherwise. If fewer than two FILEs are given, assume -h.
Exit status is 0 if any line is selected, 1 otherwise;
if any error occurs and -q is not given, the exit status is 2.
Report bugs to: bug-grep@gnu.org
GNU Grep home page: <http://www.gnu.org/software/grep/>
General help using GNU software: <http://www.gnu.org/gethelp/>
#
grep命令常用示例:(比如在file1文件中搜索error字段)
1、比较两个文件并输出相同部分的内容
grep -wf file1 file2
2、查找某一个字段
(1)、查看文件中某一个字段所在行并显示该行之前10行
grep error -B 10 file1
(2)、查看文件中某一个字段所在行并显示该行之后10行
grep error -A 10 file1
(3)、查看文件中某一个字段所在行并显示该行上下10行
grep error -C 10 file1
3、反向搜索,输出不匹配该字段的行
grep -v error file1
4、显示匹配的字段所在的行号
grep -n error file1
5、匹配多个字段
grep -e error -e successfuled file1
6、使用正则表达式搜索(例子:匹配含有“error”或者“:”和”error:“三种的字符串所在的行)
grep [error:] file1
7、排除多个条件
-E 匹配多个条件
-i 不区分大小写
-v 反向匹配
示例:排除多个条件
grep -Evi 'a|b|c' file_name
示例:匹配多个条件
grep -Ei 'a|b|c' file_name