I. Regular Expression description
· Concept: Describe data with common attributes with a string of characters
· Note: when using regular expressions for matching, be sure to use the \ transfer symbol.
· Format:
A. grep [Option] 'regular expression' file list 1 file list 2
B. | pipeline commands
· Processing Method
A. Unit of Action Processing
B. By default, the screen of the row knife that matches the regular expression is output.
C. Perform row-by-row processing on the data. After the current row is processed, the next row is automatically processed until the end of the process.
· Options
→ -- Color highlight matched data
→-Q: The matching result is not displayed.
→-C: count the number of rows that match the matched rows
→-V reverse matching
→-N: displays the row number that matches the matching row.
→- Case-insensitive letters are ignored during I matching.
→-E extension match, or use egrep
→ [] Match within the range. You can use the hyphen "-" to match any character in the range.
Ii. Matching instances
· Matching letters:
→ [Dota] # match D, O, T, or
→ [A-Z] # match any character in A to Z, the same way [A-Z]
→ [A-Z] # match all letters
· Matching numbers
→ [139] # match 1, 3, or 9
→ [0-9] # match all numbers
→ [A-Z0-9] # match all numbers and letters
· Match symbols
→ [?! _-] # Do not write the "-" symbol between other characters. Otherwise, it will be considered as a hyphen without matching.
→ \ <# Match the start of a word
→ \> # Match the end of a word
# Cat text # compile a text by yourself. Here are a few simple examples: Use | pipelines or use grep directly.
Dota
ABCD
ABCD
1234
Abcd1234
A1b2c3d4
1a2b3c4d
Thank you for watching
# Cat text | grep Dota
Dota
# Cat text | grep [Dota]
Dota
ABCD
Abcd1234
A1b2c3d4
Thank you for watching
# Grep [A-Z] Text
Dota
ABCD
ABCD
Abcd1234
A1b2c3d4
1a2b3c4d
Thank you for watching
# Grep "\ <TH" Text
Thank you for watching
# Cat text | grep "ing \>"
Thank you for watching
# Cat text | grep [-]
?! _-
# Cat text | grep-e 'dota | 808080'
Dota
1234
Abcd1234
# Grep-e 'dota | ^ 1234 $ 'text
Dota
1234
· Matching range
→ ^ [0-9] # match the row starting with a number
→ [^ 0-9] # obtain the reverse matching row, ^ indicates 'reverse select' in'
# Cat text | grep ^ [0-9]
1234
1a2b3c4d
# The matched rows will still match the rows containing numbers because the matched rows contain non-numeric characters.
# Cat text | grep [^ 0-9]
Dota
ABCD
ABCD
Abcd1234
A1b2c3d4
1a2b3c4d
Thank you for watching
?! _-
# This is the same as [^ 0-9 ].
# Grep [^ A-Z] Text
ABCD
1234
Abcd1234
A1b2c3d4
1a2b3c4d
Thank you for watching
?! _-
# Cat text | grep [^ A-Z]
Dota
ABCD
1234
Abcd1234
A1b2c3d4
1a2b3c4d
Thank you for watching
?! _-
# Cat text | grep [^ A-Z]
1234
Abcd1234
A1b2c3d4
1a2b3c4d
Thank you for watching
?! _-
3. metacharacters
· Metacharacter (Regular Expression): a regular expression is composed of one or more metacharacters.
· Metacharacter classification: character matching, frequency matching, location anchoring, grouping, etc.
→^# First line Matching
→$# Line-end matching
→^ $# Empty row match
→.# Match any single character (except line break \ n)
. $ # Match rows ending with any character
^. $ # Match the row ending with any character
^ X. y $ # match rows containing x. y. Here. represents any character
\. $ # Match rows ending with a character.
# Cat text | grep '^ do'
Dota
# Grep '34 $ 'text
1234
Abcd1234
# Cat text | grep 'W. t'
Thank you for watching
· Set the matching times and format of Regular Expressions
→ * # Match the regular expression that appears zero or multiple times
→? # Match the previous regular expression, zero or one occurrence
→ + # Match the regular expression that appears once or multiple times
→ () # Match the expression as a whole
→ {N, m} # pointing to the regular expression above, the range of matching times
{N ,}# match times greater than or equal to n
{, M} # match times less than or equal to m
{X} # match times equal to X
# Cat repetition
ABC
Abcabc
Abcabcabc
# Grep "\ (ABC \) \ {1 \}" repetition
ABC
Abcabc
Abcabcabc
# Grep "\ (ABC \) \ {3 \}" repetition
Abcabcabc
# Grep "\ (ABC \) \ {2, 3 \}" repetition
Abcabc
Abcabcabc
# Grep "\ (ABC \) \ {2, \}" repetition
Abcabc
Abcabcabc
Some practical examples
· Matching email addresses
# Grep "[a-Z0-9 _] \ {\} @ [a-Z0-9] \ {\} \ (\. com \) \ | \ (\. cn \)" 1.txt
[Email protected]
· Matching IP addresses
# Grep "\ ([0-9] \ {1, 3 \}\. \) \ {3 \} [0-9] \ {1, 3 \} "/etc/sysconfig/network-scripts/ifcfg-eth0
Ipaddr = 172.16.8.8
Netmask = 255.255.255.0
Gateway = 172.16.8.1
Dns1 = 192.168.8.1
· Match the MAC address
# Grep "\ ([0-9a-fa-f] \ {2 \ }:\) \ {5 \} [0-9a-fa-f] \ {2 \}"/etc/sysconfig/network-scripts/ifcfg-eth0
Hwaddr = 00: 0C: 29: 40: 74: D7
650) This. width = 650; "src =" http://img.baidu.com/hi/jx2/j_0066.gif "alt =" j_0066.gif "/>
This article is from the "ywcto" blog, please be sure to keep this source http://nmore.blog.51cto.com/9008175/1439067