Grep
In the past, we used grep to find lines in a file that contained certain strings, such as finding a string in a header file that fits a pattern (pattern), such as finding all strings that conform to the [email protected] pattern (that is, the email address). Requires an X character to be a letter, a number, an underscore, a decimal point.
Character class qualifier (Character Class): As in the example above X and Y, they represent a character in the pattern, but the range of values is any of a class of characters.
Quantity Qualifier (quantifier): Each part of a mailbox address can have one or more x characters.
Positional qualifiers (Achor): Describes the positional relationships between individual character classes and ordinary characters, such as mail addresses in three parts, separated by ordinary characters @ and.
Each section can be described with a character class and a quantifier, and in order to represent a positional relationship, a positional qualifier is required. (with three concepts, every time you write a regular expression, you don't get confused)
Exe
192.168.1.11234.234.04.5678123.4234.045.678abcde
The command executed is:
[12] [0-9] {0,2}\. [12] [0-9] {0,2}\. [12] [0-9] {0,2} ' exe
The result of the execution is:
650) this.width=650; "Src=" Http://s4.51cto.com/wyfs02/M00/85/A7/wKioL1erGJuzFsDiAAAV7bjU6hI574.png-wh_500x0-wm_3 -wmp_4-s_3506673568.png "title=" 1.png "alt=" Wkiol1ergjuzfsdiaaav7bju6hi574.png-wh_50 "/>
We know that C variables and shell scripting variables are defined and used differently, with different expression capabilities, and C variables have various types, and shell script variables are strings.
Character class
Character
|
Meaning
|
.
|
Match any one character
|
[]
|
Match any one of the characters in parentheses
|
-
|
Denotes a range of characters within [] parentheses
|
^
|
At the beginning of [], matching any character outside of the characters in parentheses
|
Specific Use examples
650) this.width=650; "Src=" Http://s5.51cto.com/wyfs02/M01/85/A7/wKioL1erG9TzHcusAABJ3pDoPzs082.png-wh_500x0-wm_3 -wmp_4-s_1187965337.png "title=" 2.png "alt=" Wkiol1erg9tzhcusaabj3pdopzs082.png-wh_50 "/>
Quantity qualifier
Character
|
Meaning
|
Example
|
?
|
The unit immediately preceding it matches 0 or one time
|
localhost bash]$ echo "Hello" | Grep-e--color ' hell? ' Hello |
+
|
The unit immediately preceding it should match one or more times
|
[Email protected] bash]$ echo "Helloooo" | Grep-e--color ' hello+ ' Helloooo
|
*
|
The unit immediately preceding it should match 0 or more times
|
[Email protected] bash]$ echo "Helloooo" | Grep-e--color ' hello* ' Helloooo [Email protected] bash]$ echo "Helloooo" | Grep-e--color ' helo* ' Helloooo |
Position Qualifier
Character
|
Meaning
|
^
|
Match the position of the beginning
|
$
|
Match the position at the end of the line
|
\<
|
Match the position at the beginning of the word
|
\>
|
Match the position of the end of the word
|
\b
|
Match the position of the beginning or end of a word
|
\b
|
Match the beginning and end of a word
|
Demo results
650) this.width=650; "Src=" Http://s2.51cto.com/wyfs02/M00/85/A8/wKiom1erK9KTmXhgAAB0GJE0uWc768.png-wh_500x0-wm_3 -wmp_4-s_1012968505.png "title=" 3.png "alt=" Wkiom1erk9ktmxhgaab0gje0uwc768.png-wh_50 "/>
Example:
Use the following command to find the No and standard IP numbers
Grep-e ' [1,2][0-9]{0,2}\. [0-9] {1,3}\. [0-9] {1,3}\. [0-9] {1,3} ' exe
Looking for a standard mobile phone number
Grep-e ' 1[34578][0-9]{9} ' exe
Search for No and standard mailbox numbers
Grep-e ' [123456789][0-9]{4,11} @qq \.com$ '
Shell learns the third day--grep, SED, awk