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

相關文章
相關標籤/搜索