The grep command specifies the format of the string to match the specified document grep-e ==egrep
grep egrep sed awk supports regular expressions
Regular expression: A string of the specified format to match the specified row
grep--color ' root '/etc/passwd match keyword, highlighting
grep--color-n ' root '/etc/passwd matches keywords, highlights, and displays line numbers
grep--color- c ' root '/etc/passwd only counts the total number of rows that have been matched and does not display specific content
grep--color- v ' root '/etc/passwd shows no matching content, reverse selection.
grep--color-A2 ' root '/etc/passwd displays matching rows, and the next 2 lines of the matching row are printed
grep--color-B2 ' root '/etc/passwd displays matching rows, and the top 2 rows of the matching rows are printed
grep--color-C2 ' root '/etc/passwd displays matching rows, and the top and bottom 2 lines of the matching row are also printed
grep--color- R ' root '/etc/recursively finds all files in/etc, containing the lines of the keyword
grep--color -rh ' root '/etc/recursively finds all files in/etc, containing the lines of the keyword
Remove the file path and name from the first line, and-H does not display the file path and name
In regular expressions, the meanings of special symbols are summarized as follows:
. Any of 1 characters
* * Number of characters before 0-infinity
. * any of any characters
? ? 0-1 of the first character
+ + 1 of previous characters-infinity
| Any one of the characters before and after the symbol (or relationship)
( ) () content is a whole, need to match all
{} {} is usually a number, which refers to the number of matches and can be matched multiple times.
[ ] [] The characters within, any match one can.
Example: grep ' [0-9a-za-z] ' 2.txt matches numbers and lines in English
grep '^[0-9a-za-z] ' 2.txt matches numbers and lines beginning with English
grep ' [^0-9a-za-z] ' 2.txt matches lines that are not the beginning of numbers and English
grep ' ^$ ' 2.txt matches blank line
grep ' R.O ' 2.txt matches a line containing r+ any 1 characters +o
grep ' R*o ' 2.txt matches rows containing O or ro Rro rrr...o
grep 'r.*o' 2.txt matches r start O end of line ( greedy match )
? + | () the {} symbol is special, you must use grep with the escape character \, or the parameter-e, or the command egrep
Grep-e ' r?o ' matches o or Ro rows
Grep-e ' Root|nologin ' 2.txt matches the row (or relationship) of root or Nologin
grep ' root ' 2.txt |grep ' nologin ' matches 2 times (and relationships)
Egrep '(RR)+ ' 2.txt matching RR 1-infinity (overall match)
Egrep '(RR) {1,3}' 2.txt {} is the number of matches that can be matched multiple times.
2.1-grep Filtration