其中 -E, –extended-regexp Interpret PATTERN as an extended regular expression (ERE, see below). (-E is specified by POSIX.)
fgrep is the same as grep -F.
其中 -F, –fixed-strings, –fixed-regexp Interpret PATTERN as a list of fixed strings, separated by newlines, any of which is to be matched. (-F is specified by POSIX, –fixed-regexp is an obsoleted alias, please do not use it in new scripts.)
-c, –count Suppress normal output; instead print a count of matching lines for each input file. With the -v, –invert-match option (see below), count non-matching lines. (-c is specified by POSIX.)
-A NUM, –after-context=NUM Print NUM lines of trailing context after matching lines. Places a line containing a group separator (described under –group-separator) between contiguous groups of matches. With the -o or –only-matching option, this has no effect and a warning is given.
B即 Before,使用 -B num参数,表示除了打印匹配行之外,还要打印出匹配行上面的num行
-B NUM, –before-context=NUM Print NUM lines of leading context before matching lines. Places a line containing a group separator (described under –group-separator) between contiguous groups of matches. With the -o or –only-matching option, this has no effect and a warning is given.
C即context,使用 -C num 参数,表示除了打印匹配行之外,还要打印出匹配行的上下num行
-C NUM, -NUM, –context=NUM Print NUM lines of output context. Places a line containing a group separator (described under –group-separator) between contiguous groups of matches. With the -o or –only-matching option, this has no effect and a warning is given.
-l, –files-with-matches Suppress normal output; instead print the name of each input file from which output would normally have been printed. The scanning will stop on the first match. (-l is specified by POSIX.)
-L, –files-without-match Suppress normal output; instead print the name of each input file from which no output would normally have been printed. The scanning will stop on the first match.
[baiyongan@bya grep_test]$ ll total 12 -rw-rw-r-- 1 baiyongan baiyongan 36 May 23 11:26 1.txt -rw-rw-r-- 1 baiyongan baiyongan 37 May 23 11:27 2.txt -rw-rw-r-- 1 baiyongan baiyongan 36 May 23 11:26 3.txt [baiyongan@bya grep_test]$ for i in $(ls ./); do echo context of $i && cat $i && echo; done context of 1.txt the first file this line is useless
context of 2.txt the second file this line is useless
context of 3.txt the third file this line is useless
-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 constituent 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.
1 2 3 4 5 6 7 8 9 10 11 12
[baiyongan@bya grep_test]$ cat word.txt I love daisy My gloves are red. [baiyongan@bya grep_test]$ grep love word.txt I love daisy My gloves are red. [baiyongan@bya grep_test]$ grep '\<love\>' word.txt I love daisy [baiyongan@bya grep_test]$# 使用 -w 参数,更加简单 [baiyongan@bya grep_test]$ grep -w love word.txt I love daisy [baiyongan@bya grep_test]$
[baiyongan@bya grep_test]$ cat roc.txt # roc.txt文件中,有^ ^$等 ^this is a useless line ^$this is another useless line [baiyongan@bya grep_test]$ grep '^this' roc.txt # grep会找开头为this的行,无匹配 [baiyongan@bya grep_test]$ grep '\^this' roc.txt ^this is a useless line [baiyongan@bya grep_test]$ fgrep '^this' roc.txt # fgrep会找 ^this这个字符串,有匹配 ^this is a useless line [baiyongan@bya grep_test]$