Grep(global search regular expression(RE) and print out the line,全面搜索正则表达式并把行打印出来)是一种强大的文本搜索工具,它能使用正则表达式搜索文本,并把匹配的行打印出来。 参数-a 不要忽略二进制数据。
-a< Displays the number of columns > displays the contents of the row, in addition to the row that matches the template style.
-B appears outside the row that conforms to the template style and displays the contents before the line.
-C calculates the number of columns that conform to the template style.
-c< Displays the number of columns > or-< Displays the number of columns > In addition to displaying the column that conforms to the template style, and displays the contents before the column.
-d< Action > When you specify that you want to find a directory rather than a file, you must use this parameter, otherwise the grep command returns information and stops the action.
-e< template Style > Specify a string as the template style for finding the contents of a file.
-E uses the template style as an extended normal notation, which means that extended regular expressions can be used.
-f< template File > Specify a template file with the contents of one or more template styles so that grep finds the content of the file that conforms to the template criteria, formatted as a template style for each column.
-F treats the template style as a list of fixed strings.
-G uses the template style as a normal notation.
-H does not indicate the file name that the column belongs to until it displays the column that conforms to the template style.
-h indicates the file name of the column before displaying the column that conforms to the template style.
-I ignores the difference in the case of characters.
-l lists file names that match the file contents to the specified template style.
-l lists file names for file contents that do not conform to the specified template style.
-N indicates the column number before displaying the column that conforms to the template style.
-Q does not display any information.
-r/-r the effect of this parameter is the same as specifying the "-D recurse" parameter.
-S does not display an error message.
-V Reverse lookup.
-W displays only the columns that match the whole word.
-X displays only the columns that are eligible for all columns.
-y This parameter effect is the same as "-I".
-O outputs only the portion of the file that matches.
Common usage of GREP commands
Searching for a word in the file, the command returns a line of text containing "Match_pattern":
grep match_pattern file_name
grep "Match_pattern" file_name
Find in multiple files:
grep "Match_pattern" File_1 file_2 file_3 ...
Output all rows except the-V option:
Grep-v "Match_pattern" file_name
The output contains the number of rows that match the string-N option:
grep "Text"-N file_name
Or
Cat file_name | grep "Text"-N
#多个文件
grep "Text"-N file_1 file_2
grep Silent Output:
Grep-q "test" filename
#不会输出任何信息, Failure returns a value other than 0 if the command successfully returns 0. Typically used for conditional testing.
Print a line before or after matching text:
#显示匹配某个结果之后的3行, use the-a option:
Seq 10 | grep "5"-A 3
5
6
7
8
#显示匹配某个结果之前的3行, use the-B option:
Seq 10 | grep "5"-B 3
2
3
4
5
#显示匹配某个结果的前三行和后三行, use the-C option:
Seq 10 | grep "5"-C 3
2
3
4
5
6
7
8
#如果匹配结果有多个, "--" is used as the delimiter between each matching result:
Echo-e "A\NB\NC\NA\NB\NC" | grep a-a 1
A
B
A
B
Linux Three Musketeers (grep filter)