First, what is the regular expression
A regular expression is a pattern written by a meta-character and a normal character, where the metacharacters do not represent the meaning of the character itself, but are used to express control or to function as a wildcard.
The basic regular Expression meta-character:
(1) character (range) matching
. : Matches any single character
[]: matches any single character within the specified range
[^]: matches any single character outside the specified range
[0-9],[[:d igit:]],[^0-9],[^[:d igit:]]
[A-z],[[:lower:]]
[A-z],[[:upper:]]
[[: Space:]]
[[:p UNCT:]]
[0-9a-za-z],[[:alnum:]]
[A-za-z],[[:alpha:]]
(2) Number of times match: A control is provided after the expected match character, which is used to express the number of occurrences of the preceding character specified
*: Any length, 0 times, 1 times or more
"Ab*c"
Abbc,ac
Abb
. *: Any character of any length, working in greedy mode
\?:0 Or 1 times, indicating that its left character is optional
"Ab\?c"
ABBC,
Ac,abc
\+: 1 or more times, indicating that its left character appears at least 1 times
\{m\}:m, indicating that its left character appears precisely m times
\{m,n\}: At least m times, up to N times
\{0,n\}: Up to n times
\{m,\}: At least m times
(3) Position anchoring:
^: Anchor beginning ^pattern
$: Anchor Line End pattern$
^pattern$: Use pattern to match whole line
^$: Matching Blank lines
(4) Word anchoring: a continuous string consisting of non-special characters
\<: Anchor word head, also available \b
\<pattetn or \b
\>: Anchors the ending. Also available \b
Pattern\>
\<pattern\>: Matches the entire word that Patern can match
(5) Group: \ (\)
Note: The patterns in the grouping, the characters that match during a specific match, can be memorized by grep (stored in the built-in variables, which are \1,\2,...) and therefore can also be referenced:
\1: Reference, from left to right in a pattern, matched by the first opening parenthesis and the pattern in the right parenthesis corresponding to it
\2: Reference, from left to right in a pattern, matched by the second opening parenthesis and the pattern in the right parenthesis corresponding to it
Three, grep introduction
grep (Global search Regular expression and Print out of the line) searches the regular expression globally and searches to a qualifying row of output.
grep command format:
grep [OPTIONS] PATTERN [FILE ...]
* pattern is the mode of regular expression writing
* file is to find the files, can be a space interval of multiple files, omit file when it is found in standard input
Common parameters:
-O: Show only the characters that match
---I ignore case, character-insensitive case
-V Invert selection to display rows that cannot be matched to
-e using extended regular expressions
---a# simultaneously outputs the following # lines that match the line
---b# outputs the first # lines of matching rows simultaneously
---c# simultaneously outputs the front and back # lines of the matching line
Example: grep--color=auto "Root"/etc/passwd
Iv. egrep: The command searches the input file for rows that match the pattern specified by the pattern parameter
Command format: Egrep PATTERN FILE ...
(1) Character matching:
.: Matches any single character
[]: matches any single character within the specified range
[^]: matches any single character outside the specified range
(2) Number of matches:
*: Any time
?: 0 or 1 times
+: at least 1 times
{m}: exact match m times;
{M,n}: At least m times, up to n times;
{m,}: at least m times;
{0,n}: up to n times;
(3) Position anchoring:
^
$
\<, \b
\>, \b
(4) Group:
()
Citation: \1, \2, ...
Or:
A|b:a or B
or all the contents on both sides;
Example:
1. Display lines beginning with uppercase or lowercase s in the/proc/meminfo file
[Email protected]/]# grep-e "^[ss]"/proc/meminfo
swapcached:0 KB
swaptotal:2097148 KB
swapfree:2097148 KB
shmem:280 KB
slab:60008 KB
sreclaimable:34660 KB
sunreclaim:25348 KB
2. Display the user whose default shell is/bin/bash in the/etc/passwd file
[[email protected]/]# grep "/bin/bash$"/etc/passwd | Cut-d:-f1
Root
Mandriva
Mageia
Mandriva1
Opstack
Nova
Hadoop
Mysql1
User1
User10
User11
A detailed description of the use of regular expressions