linux grep

linux grep命令中的-w和-R或-r参数。这两个参数在通常教程中不多提到,但在特定的时候用处很大。 html

-w, –word-regexp
Select only those  lines  containing  matches  that  form  whole
words.   The  test is that the matching substring must either be
at the beginning of the line, or preceded  by  a  non-word  con-
stituent  character.  Similarly, it must be either at the end of
the line or followed by a non-word constituent character.  Word-
constituent  characters are letters, digits, and the underscore. linux

//意思就是精确匹配,匹配单词还不是字符串,如想匹配“is”,”this”就不会被匹配。实例以下: git

[root@GoGo ~]# cat goface.txt ui

goface this

gofaceme spa

[root@GoGo ~]# grep ’goface’ goface.txt regexp

goface orm

gofaceme htm

[root@GoGo ~]# grep -w ’goface’ goface.txt blog

goface

怎么样,有实例一会儿就看明白了吧。

-R, -r, –recursive
Read all files under each directory, recursively; this is equiv-
alent to the -d recurse option.

//-r/-R选项是递归搜索, 查找你搜索的目录或子目录下的全部含有某个你要找的文件.

[root@GoGo ~]# grep -R ’goface’ /root

/root/goface.txt:goface

/root/goface.txt:gofaceme

遍历/root下全部文件匹配goface。

若是寻找的目录下文件太多或含有大文件,可以使用如下参数或结合find命令。

–include=PATTERN
Recurse in directories only searching file matching PATTERN.

–exclude=PATTERN
Recurse in directories skip file matching PATTERN.

相关阅读:

gnu grep manual:http://www.gnu.org/s/grep/manual/grep.html

linux find命令实例:http://blog.51osos.com/linux/linux-find-command/

linux grep命令参数:http://bbs.51osos.com/thread-3937-1-1.html

linux grep命令实例:http://bbs.51osos.com/thread-533-1-1.html

相关文章
相关标签/搜索