Summary of the regular expression
Describe, using a single character string matching conform to a particular statement rules, is composed of common characters and special characters, regular expressions are widely used in the scripting, text editor,
Regular expression shorthand for regex, regexp, RE,
In most of the regular expression of language is included among the two forward slashes "/"
Regular expressions are a feature of greed matching: try to match the longest things
Regular expression classification
Based on regular expression
\ escape characters, such as: "the \!" The logical not! As a common character, \ can also fold line
Example 1:
/root @ localhost ~ # ls - l/home \
/TMP
Example 2:
[root @ localhost ~] # grep "b. * in" a.t xt
12 bin 34
23 sb. * in 23
44 binary 49
23 binlog 23
[root @ localhost ~] # grep "b \. \ * in" a.t xt
23 sb. * in 23
1
2
3
4
5
6
7
8
9
10
11.
^ matching string starting position, in order to... For the beginning of the
$matches the location of the end of the string, in order to... For the end of the
^ $said empty lines, not space
Example:
/root @ localhost ~ # cat a.t xt
Aa
Aa bb aa
A, a, b
[root @ localhost ~] # grep "^ aa $" a.t xt
Aa
1
2
3
4
5
6
7
The match any string
* matching face expression (characters) 0 or more times before
Example:
/root @ localhost ~ # grep "a 0 *" a.t xt
Aa 0
00 aa
00 aa bb 0000 aa
B, a 000
. * matches any string
[the list] match the list in the list of one character at a time, for example: [ABC], [a-z], [a zA - Z0-9]
Example:
/root @ localhost ~ # ifconfig ens32 | grep "TX (PE)
"TX packets 1776 bytes (202.0 206946 KiB)
TX errors 0 dropped overruns carrier collisions 0 0 0 0
[^ list] match any one character at a time, in the list table, for example: [^ a-z], [^ 0-9], [^ a - Z0-9]
Example:
/root @ localhost ~ # ifconfig ens32 | grep "TX [a - f]"
TX errors 0 dropped overruns carrier collisions 0 0 0 0
/root @ localhost ~ # ifconfig ens32 | grep "TX [^ a - f]"
TX packets 1814 bytes (205.7 210662 KiB)
\ \ {n} before matching face expression n
Example:
/root @ localhost ~ # grep "a 0 \ \ {2}" a.t xt
00 aa
00 aa bb 0000 aa
B, a 000
\ {n, \} matching face expression at least n times before
/root @ localhost ~ # grep "a 0 \ {2, \}" a.t xt
00 aa
00 aa bb 0000 aa
B, a 000
\ {n, m \} before matching face expression at least n times, maximum m times
\ & lt; Word first monding: fixed term first
\ & gt; The word not monding: fixed suffix
Example:
/root @ localhost ~ # grep "\ & lt; A \ & gt;" A.t xt
B, a 000
1
2
3
4
5
6
7
8
9
10
11.
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
Escape character meaning
\ a ring (BEL)
\ b backspace (BS), the current location will be moved to the previous column
\ n (LF) is a new line will be moved to the beginning of a line under the current position
\ r carriage return (CR) move the current position to the opening bank
\ t horizontal TAB (HT) transferred to the next TAB position
Vertical tabulation \ v (VT)
\ represents a backslash character ""
Extended regular expression
+ matches before the face expression is one or more
? Before the match zero or face expression 1
() the string as a whole in parentheses
| string match in the form of or
1
2
3
4
Linux is commonly used in text processing tools
Grep match, query (filtering)
Sed to edit (add, delete, modify)
Awk text formatting (string extraction)
Note:
Linux regular file with behavior unit commonly
Alias grep='grep -- color=auto
'Pay attention to the character set, LANG=C
The wildcard
Regular expressions, and we use the command line there are essential differences between the wildcard,
The wildcard is generally deal with file name
Ls -l *. TXT
* : any arbitrary string length, belongs to the wildcard,
? : a single arbitrary string, belongs to the wildcard,
The parameters of the grep command:
-v ruled out match (against) the content of the
- E extended regular expression support=egrep
/root @ localhost ~ # grep - Ev "^ $| #"/etc/HTTPD/conf/HTTPD. Conf
ServerRoot "/etc/HTTPD"
Listen to 80
Include the conf. Modules. D/*. Conf
1
2
3
4
- I ignore case
/root @ localhost ~ # cat a.t xt
AAAA
/root @ localhost ~ # grep -i "a" a.t xt
AAAA
/root @ localhost ~ #
1
2
3
4
5
-o output only match the content of the
/root @ localhost ~ # grep -o "root"/etc/passwd
The root
The root
1
2
3
-- color=auto match the content of the display color (with few alias view alias)
- n at the show line Numbers
/root @ localhost ~ # grep - n "root"/etc/passwd
1: root: x: 0-0: root:/root:/bin/bash
10: operator: x: 11:0: operator:/root/sbin/nologin:
43: admin: x: : 1000-1000 root:/home/admin:/bin/bash
1
2
3
4
- q matching content does not display (quiet output, general writing the script and not output to the screen)
- w will filter conditions as word to match the
/root @ localhost ~ # grep - w/etc/passwd "bin"
Root: x: 0-0: root:/root:/bin/bash
Bin: x: 1:1: bin:/bin:/sbin/nologin
Sync: x: 5-0: sync:/sbin/bin/sync:
Admin: x: 1000-1000: root:/home/admin:/bin/bash
1
2
3
4
5
/root @ localhost ~ # grep "bin"/etc/passwd
Root: x: 0-0: root:/root:/bin/bash
Bin: x: 1:1: bin:/bin:/sbin/nologin
The daemon: x: 2-2: daemon:/sbin/sbin/nologin:
1
2
3
4
- c output matching rows only count
/root @ localhost ~ # grep -c "root"/etc/passwd
3
1
2
Little knowledge:
Nohup running script//do not rely on end run the script, will put the information in nohup file
CodePudding user response:
Thanks for sharing, the basic knowledge to personal blog, pleaseCodePudding user response: