目录
grep全称是Global Regular Expression Print,表示全局正则表达式版本,它的使用权限是所有用户。grep在数据中查找出一个字符串时,是以整行为单位来进行数据选取的。grep是linux中很常用的一个命令,主要功能就是进行字符串数据的对比,能使用正则表达式搜索文本,并将符合用户需求的字符串打印出来。
1、grep --h查看命令的使用方式
用法: grep [选项]... PATTERN [FILE]...
在每个 FILE 或是标准输入中查找 PATTERN。
默认的 PATTERN 是一个基本正则表达式(缩写为 BRE)。
例如: grep -i 'hello world' menu.h main.c
正则表达式选择与解释:
-E, --extended-regexp PATTERN 是一个可扩展的正则表达式(缩写为 ERE)
-F, --fixed-strings PATTERN 是一组由断行符分隔的定长字符串。
-G, --basic-regexp PATTERN 是一个基本正则表达式(缩写为 BRE)
-P, --perl-regexp PATTERN 是一个 Perl 正则表达式
-e, --regexp=PATTERN 用 PATTERN 来进行匹配操作
-f, --file=FILE 从 FILE 中取得 PATTERN
-i, --ignore-case 忽略大小写
-w, --word-regexp 强制 PATTERN 仅完全匹配字词
-x, --line-regexp 强制 PATTERN 仅完全匹配一行
-z, --null-data 一个 0 字节的数据行,但不是空行
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 ignored for backwards compatibility
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'
-D, --devices=ACTION how to handle devices, FIFOs and sockets;
ACTION is `read' or `skip'
-R, -r, --recursive equivalent to --directories=recurse
--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
--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)
-u, --unix-byte-offsets report offsets as if CRs were not there (MSDOS)
‘egrep’即‘grep -E’。‘fgrep’即‘grep -F’。
直接使用‘egrep’或是‘fgrep’均已不可行了。
不带 FILE 参数,或是 FILE 为 -,将读取标准输入。如果少于两个 FILE 参数
就要默认使用 -h 参数。如果选中任意一行,那退出状态为 0,否则为 1;
如果有错误产生,且未指定 -q 参数,那退出状态为 2。
2、简单实例
#netstat -nap|grep 80
上面命令查询监听80端口的行。
#grep -n "world" hello.txt
查询文件hello.txt中含有"world"字符串的行,并显示行号。
#grep -n "printf" main.c hello.c world.c
在源码文件main.c、hello.c和world.c中查询调用了printf函数的行,并显示行号。
#grep -n "printf" *
在当前目录的所有文件中查询调用了printf函数的行,并显示行号。也可以指定路径,比如:/usr/local/include/*。
#grep -r "printf" *
在当前目录及所有子目录下递归查找调用了printf函数的行,并显示行号。
3、正则用法
grep patten正则表达式主要元字符及使用如下表:
元字符 | 功能 | 例子 | 匹配什么 |
^ | 锚定行的开始 | /^love/ | 匹配所有以love开头的行 |
$ | 锚定行的结束 | /love$/ | 匹配所有以love 结束的行 |
. | 匹配任意一个字符 | /l..e/ | 匹配这样的行,这些行包含这样的字符:第一个字符是l,紧跟着两个字符,然后是e |
* | 代表0个或多个先前字符 | /*love/ | 匹配所有这样的行,有0个或多个空格,空格后跟着love |
[] | 匹配字符组中的一个字符 | /[Ll]ove/ | 匹配所有包含love或者Love的行 |
[^] | 匹配一个不在范围内的字符 | /[^A-Z]ove/ | |
< | 锚定单词的开始 | <love | 匹配所有这样的行,这些行包含以love开头的单词(vi和grep支持这个功能) |
> | 锚定单词的结束 | Love> | 匹配所有这样的行,这些行包含以love结束的单词(vi和grep支持这个功能) |
(..) | 标记后面用到的匹配字符 | /(love)able1rs/ | 最多可以使用9个标签。第一个标签是模板最左边的部分。在本例子中,模板love保存的标签为1,后面的1指得就是love;本例子搜索的是这样的行,这些行包含这样的字符,在Loveabel后面跟着lovers |
x{m} | M次复制字符x | ||
x{m,} | 至少m次复制字符x | ||
x{m,n} | 至少m次,至多n次复制字x | ||
w | 文字和数字字符,[A-Za-z0-9] | Lw*e | 匹配一个L字符,紧跟着0个或多个文字或数字字符,然后是e |
W | 同上 | ||
b | 单词分界线 | bloveb | 仅仅匹配单词love |
简单实例如下:
#ls -l | grep '^a'
通过管道过滤ls -l输出的内容,只显示以a开头的行。
#grep 'test' d*
显示所有以d开头的文件中包含test的行。
#grep '[a-z]\{5\}' aa
显示所有包含每个字符串至少有5个连续小写字符的字符串的行。
#grep 'w\(es\)t.*\1' aa
如果west被匹配,则es就被存储到内存中,并标记为1,然后搜索任意个字符(.*),这些字符后面紧跟着另外一个es(\1),找到就显示该行。如果用egrep或grep -E,就不用"\"号进行转义,直接写成'w(es)t.*\1'就可以了。
4、其它家族egrep和fgrep
grep家族除了有grep命令外,还包括egrep和fgrep。
egrep是grep的扩展,支持更多的正则元字符,具体如下:
+ | 匹配一个或者多个先前字符 | [a-z]+ove | 匹配一个或者多个小写字符且后面是ove的, |
? | 匹配0个或者多个先前字符 | Lo?ve | 匹配L后面有一个或者没有o,然后是ve |
a|b|c | 匹配a或b或c | Love|hate | 匹配love或hate其中一个 |
() | 字符组 | Love(able|rs)(ov)+ | 匹配loveable或lovers,匹配一个或多个ov |
(…)(…)12 | 标记匹配字符串 | (love)ing | 标签标记出寄存器的一部分,并稍后替换模板。该模板叫做1,并可反复引用。在表达式中最多可以使用9个这样的标签。例如,模板love被保存在寄存器1中并稍后替换标签叫做1 |
x{m} | 重复字符X,m次,至少m,或者m次和n次之间。 | ||
x{m,} | |||
x{m,n} |
fgrep是fixed grep或fast grep,它把所有的字母都看作单词,即正则元字符不再特殊,而仅仅表示其自身的字母意思。
参考: