前言
The grep utility searches any given input files, selecting lines that match one or more patterns.
grep
is used for simple patterns and basic regular expressions (BREs);
SYNOPSIS
grep [-abcdDEFGHhIiJLlmnOopqRSsUVvwxZ] [-A num] [-B num] [-C[num]] [-e pattern] [-f file] [--binary-files=value] [--color[=when]] [--colour[=when]] [--context[=num]] [--label] [--line-buffered] [--null]
[pattern] [file ...]
The following options are available:
- 主要参数
-C[num, --context=num] Print num lines of leading and trailing context surrounding each match. The default is 2 and is equivalent to -A 2 -B 2. Note: no whitespace may be given between the option and its argument. 输出匹配行的计数 -i, --ignore-case Perform case insensitive matching. By default, grep is case sensitive.不区分大小写 -h, --no-filename Never print filename headers (i.e. filenames) with output lines.查询多文件时不显示文件名。 -l, --files-with-matches Only the names of files containing selected lines are written to standard output. grep will only search a file until a match has been found, making searches potentially less expensive. Pathnames are listed once per file searched. If the standard input is searched, the string ``(standard input)'' is written. 查询多文件时只输出包含匹配字符的文件名。 -n, --line-number Each output line is preceded by its relative line number in the file, starting at line 1. The line number counter is reset for each file processed. This option is ignored if -c, -L, -l, or -q is speci- fied.显示匹配行及行号。 -s, --no-messages Silent mode. Nonexistent and unreadable files are ignored (i.e. their error messages are suppressed).不显示不存在或无匹配文本的错误信息。 -V, --version -v, --invert-match Selected lines are those not matching any of the specified patterns. 显示不包含匹配文本的所有行。
pattern正则表达式主要参数:
- pattern正则表达式主要参数:
\: 忽略正则表达式中特殊字符的原有含义。
^:匹配正则表达式的开始行。
$: 匹配正则表达式的结束行。
\<:从匹配正则表达 式的行开始。
\>:到匹配正则表达式的行结束。
[ ]:单个字符,如[A]即A符合要求 。
[ - ]:范围,如[A-Z],即A、B、C一直到Z都符合要求 。
.:所有的单个字符。
* :有字符,长度可以为0。
EXAMPLES
- grep -r magic /usr/src
#显示/usr/src目录下的文件(包含子目录)包含magic的行
- xargs
find . -name "*" | xargs grep "iosre" > ./cqtest.txt
ps -e grep Safari
see also
/Users/devzkn/bin/knpost grep file pattern searcher -t Terminal #原来""的参数,需要自己加上""