grep Regular Expression
1. grep definition
Globally search a Regular Expression and Print
Global search (matches regular expressions) and print
Extended command:
Egrep: Extended grep, equivalent to the GREP-E command, uses an extended regular match.
Fgrep:fast grep, which does not support regular matching grep, can only match the character's original meaning.
The difference between grep and Egrep is that Egrep supports more extended regular
grep parameters (#代表数字)
-V: Show rows that do not match
-I: Ignore letter case
-O: Print only matching characters
-e: extending regular expressions
-A #: Displays matching lines and # lines after matching lines
-B #: Show matching lines and # lines before matching lines
-C #: Display matching lines and lines before and after matching lines
--color: Defines the color of matching characters
Color tags match the characters
Alias grep= ' grep--color=auto '
Alias egrep= ' Egrep--color=auto '
Permanent effect Modify File/ETC/BASHRC Add the above two lines of content
Regular expressions
One, character matching
. : Matches one character
[]: matches any single character within the range
[^]: matches any single character outside the range
Cases:
[0-9] [[:d Igit:]] Match numbers
[A-z] [[: Lower:]] Match lowercase letters
[A-z] [[: Upper:]] matches uppercase letters
[[: Space:]] matches all whitespace characters (spaces, tabs, new lines)
[0-9a-za-z] [[: Alnum:]] match letters and numbers
[A-za-z] [[: Alpha:]] Match letter
[[:p UNCT:]] punctuation marks
Second, the number of matches
*: Any number of times
. *: Any character of any length
\?:0 times or 1 times
\+:1 Times or more
\{m\}:m Times
\{m,n\}: At least m times, up to N times
\{0,n\}: Up to n times
\{m,\}: At least m times
Third, positioning matching
^: Match the beginning of the line
$: Match end of line
\<,\b: match the first word
\>,\b: Match word endings
Iv. grouping and referencing
\ (\): A group within parentheses
\1,\2,...... Reference grouping, 1 is the first parenthesis content, 2 is the second parenthesis content ...
Metacharacters Summary:
Regular Expressions and grep
Character matching:. [] [^]
Number of matches: * \+ \? \{\}
Location matching: ^ $ \< \>
Group and back references: \ (\) \1,\2 ...
Extended Regular Expressions and egrep
Character matching:. [] [^]
Number of Matches: * +? {}
Location matching: ^ $ \< \>
Group and Back references: () \1,\2 ...
Or: a|b
Cases:
1. Character Matching
Normal characters: # grep ' Root '/etc/passwd
.: # grep ' R.. T '/etc/passwd
[]:# grep ' [RT]. '/ETC/PASSWD
[^]:# grep ' [^a-z] '/etc/passwd
2, the number of matches:
*:# grep ' ro*t '/etc/passwd
. *:# grep ' r.*t '/etc/passwd
\+:# grep ' ro\+t '/etc/passwd
+:# egrep ' ro+t '/etc/passwd
# grep-e ' ro+t '/etc/passwd
\?:# grep ' ro\?t '/etc/passwd
?: # egrep ' ro?t '/etc/passwd
# grep-e ' ro?t '/etc/passwd
\{\}:# grep ' ro\{2\}t '/etc/passwd
# grep ' ro\{0,2\}t '/etc/passwd
{}:# egrep ' ro{2}t '/etc/passwd
# grep-e ' ro{2}t '/etc/passwd
# egrep ' ro{0,2}t '/etc/passwd
# grep-e ' ro{0,2}t '/etc/passwd/
3, positioning matching:
^:# grep ' ^r '/etc/passwd
$:# grep ' h$ '/etc/passwd
\<:# grep ' \<ro '/etc/passwd
\>:# grep ' ot\> '/etc/passwd
4. Grouping and referencing:
\ (\): # grep ' ^\ (\<[a-z]\{4\}\>\). *\1$ '/etc/passwd
(): # egrep ' ^ (\<[a-z]{4}\>). *\1$ '/etc/passwd
This article is from the "Start Again" blog, please be sure to keep this source http://4708705.blog.51cto.com/4698705/1684146
grep and regular expression