################################################
Basic Regular Expressions: Grep uses patterns defined by regular expressions to filter text
Grep
-I: Case insensitive
--color
-V: Displays any rows that are not matched
-O: Show only strings that are matched
The-e extension is exactly the expression =egerp
-a#: #为数字
-b#:
-c#:
[[email protected] testcp]# grep-o ' root '/etc/passwd--color
Root
Root
Root
Root
Metacharacters
.: Matches any single character
[]: matches a single character within a specified range
[^]: matches any single character outside the specified range
Number of matches:
*: matches the preceding character any time
. *: Matches any string
\? Match the preceding character 1 or 0 times
\{m,n\}: Matches the preceding character m-n times
Location Anchorage
^: Anchoring the beginning of the line
\< or \b: Any character after the first anchor word must appear as a word header
\<root
$: Anchor Line End
\> or \b: Any character in front of the anchor ending appears as a word in the tail
Root\>
\ (abc\) as content occurrence and extended regular expression similarity
Support for back-to-reference
################################################
Extended Regular expression: REGular expression regexp
Metacharacters
.: Matches any single character
[]: matches a single character within a specified range
[^]: matches any single character outside the specified range
Number of matches:
*: matches the preceding character any time
. *: Matches any character
\? Match the preceding character 1 or 0 times
+: Match the preceding character at least 1 times
{M,n}: match the preceding character
[Email protected] ~]# Cat/tmp/t.txt
It is open no opening
###########
Location Anchorage
^: Anchoring the beginning of the line
###########
$: Anchor Line End
###########
^$: Blank Line
###########
\< or \b: Any character after the first anchor word must appear as a word header
\<root
###########
\> or \b: Any character in front of the anchor ending appears as a word in the tail
Root\>
###########
(AB) * Grouping (usually expanded with parentheses as the word appears)
[[email protected] ~]# grep ' (ABC) * '-e--color/tmp/t.txt
Ab
Abc
AaB
Aaac
Support for back reference \1,\2
[[email protected] ~]# grep-e ' (open). *\1 '/tmp/t.txt--color
It is open no opening
###########
|: OR OR
Write 1-255 in full expression
\<[1-9]| [1-9] [0-9]|1[0-9][0-99]|2[0-4][0-9]|25[0-5]\>
Filter out numbers between 0-255 in the NIC configuration file
Ifconfig | Grep-e ' (\<[0-9]|[ 1-9][0-9]|1[0-9][0-99]|2[0-4][0-9]|25[0-5]\>) '--color
[Email protected] ~]# CAT/ETC/PASSWD | grep ' nologin\> '--color
Bin:x:1:1:bin:/bin:/sbin/nologin
Daemon:x:2:2:daemon:/sbin:/sbin/nologin
Adm:x:3:4:adm:/var/adm:/sbin/nologin
Lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
Mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
Uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin
This article is from the "James Zhan Linux Advanced ops" blog, so be sure to keep this source http://jameszhan.blog.51cto.com/10980469/1875541
-12,grep regular expressions and extended regular expressions for Linux basic articles