linux grep

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

-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.

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

[root@GoGo ~]# cat goface.txt

goface

gofaceme

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

goface

gofaceme

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

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

你可能感兴趣的:(linux,grep)