Regular Express : Regular Expression
Use " a string of symbols " to describe data with common properties
Introduction to the grep command line
type annotation
grep Standard grep command, supports basic regular expressions
egrep Extended grep command, supports basic and extended regular expressions
fgrep Fast grep command, does not support regular expressions, matches the literal meaning of the string
grep processing data order
Behavioral processing unit
Process the data line by line, automatically process the next line after processing the current line, and automatically process all behaviors
By default, lines with data matching the regular expression are output to the screen
Without options, data is processed by default
After processing the current line, automatically process the next line until the last line
egrep text filtering
egrep [options] ' regex ' file pre-command | egrep [options] ' regular expression ' egrep is equal to grep -E. Indicates that extended regular expressions are allowed
Common command options:
Parameter option comments - i ignore letter case - v conditional inversion - c count the number of matching lines - q silent, no output, generally used for detection; see the return value of $?, if it is 0, it means there is a match, otherwise no Match - n display the number of lines where the matching result is located - color marked red to display the matching string
basic metacharacters
Example of type meaning ^ matches the beginning of a line ^ abc starts with abc ^ # lines starting with # (such as comment lines) $ matches line endings abc $ lines ending with abc ^ $ Blank line . matches a single character. Any single character except a newline (\n) ? match at most once * matches any number of times of the preceding ordinary character a* 0 or more consecutive a (abc) * 0 or more consecutive abc . * Any string of any length + at least one match a + one or more consecutive a (abc) + one or more consecutive abc
Metacharacters {}: limit the number of times the expression is matched
Type Meaning Example Description {n} matches n times (ab){ 3 } matches ababab {n,m} matches n -m times (ab){ 1 , 3 } matches ab, abab, ababab {n,} matches at least n times (ab){ 2 } matches 2 or more consecutive abs
metacharacter []
set of matching characters Adding ^ inside [] can be negated; the " ^ " symbol means matching the beginning of the line, but when the "^" symbol is placed in the " [] " symbol, it is no longer matching the beginning of the line, but means negating
Example |
illustrate |
[acl45_?] |
matches a, c, l, 4, 5, _, ? |
[a-z] |
matches any lowercase letter |
[A-Z] |
matches any uppercase letter |
[0-9] |
match any number |
[a-Z0-9] |
matches any letter or number |
[^A-Z] |
matches lines that include non-capital letters |
^[^a-z] |
Match lines that do not start with a lowercase letter |
[m~n] |
match m and n |
[:space:] |
whitespace |
[:point:] |
Punctuation |
[:lower:] |
Lower case letters |
[:upper:] |
uppercase letter |
[:alpha:] |
uppercase and lowercase letters |
[:digit:] |
number |
[:alnum:] |
Numbers and uppercase and lowercase letters |
other metacharacters
Type Meaning Example Description () The combination is the whole ab{ 3 } matches ab, abb, abbb (ab){ 3 } matches ab, abab, ababab | or root| bin matches root, bin \b word boundary\broot\b matches the word root, not keroot, rooty and other strings \ < start of word \< th matches words starting with th \ > end of word \<root\> same as \broot\b
\ is an escape symbol, which can give special meaning to some ordinary characters, or turn some special characters into ordinary characters
\<\> : Exact match symbol, which uses the "\" symbol to shield the "<>" symbol, such as \<the\> means to match the word exactly , but not the word that matches the character of the
() is usually used in conjunction with the | symbol to indicate a set of optional characters