Linux regular expressions-Basic Regular Expressions (based on grep)
Linux regular expression:
In short, regular expressions are a set of rules and methods defined to process a large number of strings. For example, suppose @ represents 123456 ,! Represents abcde. the system administrator can quickly filter, replace, or output the desired characters by defining these special symbols.
Notes for using regular expressions:
1. linux regular expressions are generally processed in the unit of action.
2. alias grep = 'grep -- color = auto'. The lecture is based on grep. (Note: to make the following case clearer, run this command before doing the following case)
3. Note the character set LC_ALL = C.
Linux regular expression. It is mainly the regular expression of the three muskeys of awk, sed, and grep (egrep.
A. Basic Regular Expression (based on grep)
1. ^ a indicates searching for content starting with
2. a $ indicates searching for content ending with
3. ^ $ indicates empty rows, not Spaces
4 .. represents and can only represent any one character
5. \\. it only indicates the vertex itself. escape characters, so that characters with special identities can be removed from the vest,
6. * repeat 0 or multiple first characters
7. * match all characters. ^. * Starts with any letter and ends with any letter. * $
^. * According to the previous single character, we know that ^. * matches content starting with any number of strings.
8. [abc] match any character in the character set [a-z].
9. [^ abc] match content that does not contain any character after ^
[^ A-z] does not contain lowercase letters
[^ 0-9] does not contain numbers
10. a \ {n, m \} repeats n to m times, the previous repeating character. If you use egrep, you can remove the diagonal line.
\ {N, \} should be repeated at least n times, with the previous repeated characters. If you use egrep, you can remove the diagonal line.
\ {N \} repeats n times, the previous character is repeated. If you use egrep, you can remove the diagonal line.
\ {, M} \ repeat a maximum of m times-it is best not to use it.
[Root @ nginx_back ~] # Grep ". *" test. log = matches 0 or more, so there are empty rows.
Linux regular expression:
In short, regular expressions are a set of rules and methods defined to process a large number of strings. For example, suppose @ represents 123456 ,! Represents abcde. the system administrator can quickly filter, replace, or output the desired characters by defining these special symbols.
Notes for using regular expressions:
1. linux regular expressions are generally processed in the unit of action.
2. alias grep = 'grep -- color = auto'. The lecture is based on grep.
3. Note the character set LC_ALL = C.
Linux regular expression. It is mainly the regular expression of the three muskeys of awk, sed, and grep (egrep.
A. Basic Regular Expression (based on grep)
1. ^ a indicates searching for content starting with
2. $ a indicates searching for content ending with
3. ^ $ indicates empty rows, not Spaces
4.
[Root @ nginx_back ~] # Grep "." test. log = match any character, at least one, so there is no blank line.
Linux regular expression:
In short, regular expressions are a set of rules and methods defined to process a large number of strings. For example, suppose @ represents 123456 ,! Represents abcde. the system administrator can quickly filter, replace, or output the desired characters by defining these special symbols.
Notes for using regular expressions:
1. linux regular expressions are generally processed in the unit of action.
2. alias grep = 'grep -- color = auto'. The lecture is based on grep.
3. Note the character set LC_ALL = C.
Linux regular expression. It is mainly the regular expression of the three muskeys of awk, sed, and grep (egrep.
A. Basic Regular Expression (based on grep)
1. ^ a indicates searching for content starting with
2. $ a indicates searching for content ending with
3. ^ $ indicates empty rows, not Spaces
4.
[Root @ nginx_back ~] # Grep-v "^ $" test. log
Linux regular expression:
In short, regular expressions are a set of rules and methods defined to process a large number of strings. For example, suppose @ represents 123456 ,! Represents abcde. the system administrator can quickly filter, replace, or output the desired characters by defining these special symbols.
Notes for using regular expressions:
1. linux regular expressions are generally processed in the unit of action.
2. alias grep = 'grep -- color = auto'. The lecture is based on grep.
3. Note the character set LC_ALL = C.
Linux regular expression. It is mainly the regular expression of the three muskeys of awk, sed, and grep (egrep.
A. Basic Regular Expression (based on grep)
1. ^ a indicates searching for content starting with
2. $ a indicates searching for content ending with
3. ^ $ indicates empty rows, not Spaces
4.
[Root @ nginx_back ~] # Grep-vn "^ $" test. log
1: linux regular expression:
2: in short, regular expressions are a set of rules and methods defined to process a large number of strings. For example, suppose @ represents 123456 ,! Represents abcde. the system administrator can quickly filter, replace, or output the desired characters by defining these special symbols.
3: Notes for using regular expressions:
. Linux regular expressions are generally processed in the unit of action.
. Alias grep = 'grep -- color = auto'. The lecture is based on grep.
. Pay attention to the character set, LC_ALL = C
7: linux regular expression. It is mainly the regular expression of the three muskeys of awk, sed, and grep (egrep.
10: a. Basic Regular Expression (based on grep)
11: 1. ^ a indicates searching for content starting with
12: 2. $ a indicates searching for content ending with
13: 3. ^ $ indicates empty rows, not Spaces
14: 4.
[Root @ nginx_back ~] # Grep-v n "^ $" test. log
Grep: ^ $: No file or directory
Test. log: in short, regular expressions are a set of rules and methods defined to process a large number of strings. For example, suppose @ represents 123456 ,! Represents abcde. the system administrator can quickly filter, replace, or output the desired characters by defining these special symbols.
Note:
Test. log: 2. alias grep = 'grep -- color = auto'. The lecture is based on grep.
Test. log: 3. Note the character set LC_ALL = C.
Test. log:
Test. log:
Test. log: a. Basic Regular Expression (based on grep)
Test. log: 1. ^ a indicates that the content starting with a is searched.
Test. log: 2. $ a indicates that the content ending with a is searched.
Test. log: 3. ^ $ indicates empty rows, not Spaces
Test. log: 4.
[Root @ nginx_back ~] # Grep-v-n "^ $" test. log
1: linux regular expression:
2: in short, regular expressions are a set of rules and methods defined to process a large number of strings. For example, suppose @ represents 123456 ,! Represents abcde. the system administrator can quickly filter, replace, or output the desired characters by defining these special symbols.
3: Notes for using regular expressions:
. Linux regular expressions are generally processed in the unit of action.
. Alias grep = 'grep -- color = auto'. The lecture is based on grep.
. Pay attention to the character set, LC_ALL = C
7: linux regular expression. It is mainly the regular expression of the three muskeys of awk, sed, and grep (egrep.
10: a. Basic Regular Expression (based on grep)
11: 1. ^ a indicates searching for content starting with
12: 2. $ a indicates searching for content ending with
13: 3. ^ $ indicates empty rows, not Spaces
14: 4.
[Root @ nginx_back ~] # Grep "$" test. log
Linux regular expression:
In short, regular expressions are a set of rules and methods defined to process a large number of strings. For example, suppose @ represents 123456 ,! Represents abcde. the system administrator can quickly filter, replace, or output the desired characters by defining these special symbols.
Notes for using regular expressions:
1. linux regular expressions are generally processed in the unit of action.
2. alias grep = 'grep -- color = auto'. The lecture is based on grep.
3. Note the character set LC_ALL = C.
Linux regular expression. It is mainly the regular expression of the three muskeys of awk, sed, and grep (egrep.
A. Basic Regular Expression (based on grep)
1. ^ a indicates searching for content starting with
2. $ a indicates searching for content ending with
3. ^ $ indicates empty rows, not Spaces
4.
[Root @ nginx_back ~] # Grep "C $" test. log
3. Note the character set LC_ALL = C.
[Root @ nginx_back ~] # Grep "." test. log
Linux regular expression:
In short, regular expressions are a set of rules and methods defined to process a large number of strings. For example, suppose @ represents 123456 ,! Represents abcde. the system administrator can quickly filter, replace, or output the desired characters by defining these special symbols.
Notes for using regular expressions:
1. linux regular expressions are generally processed in the unit of action.
2. alias grep = 'grep -- color = auto'. The lecture is based on grep.
3. Note the character set LC_ALL = C.
Linux regular expression. It is mainly the regular expression of the three muskeys of awk, sed, and grep (egrep.
A. Basic Regular Expression (based on grep)
1. ^ a indicates searching for content starting with
2. $ a indicates searching for content ending with
3. ^ $ indicates empty rows, not Spaces
4.
[Root @ nginx_back ~] # Grep "\." test. log
In short, regular expressions are a set of rules and methods defined to process a large number of strings. For example, suppose @ represents 123456 ,! Represents abcde. the system administrator can quickly filter, replace, or output the desired characters by defining these special symbols.
1. linux regular expressions are generally processed in the unit of action.
2. alias grep = 'grep -- color = auto'. The lecture is based on grep.
3. Note the character set LC_ALL = C.
1. ^ a indicates searching for content starting with
2. $ a indicates searching for content ending with
3. ^ $ indicates empty rows, not Spaces
4.
[Root @ nginx_back ~] # Grep "rep" test. log
2. alias grep = 'grep -- color = auto'. The lecture is based on grep.
Linux regular expression. It is mainly the regular expression of the three muskeys of awk, sed, and grep (egrep.
A. Basic Regular Expression (based on grep)
[Root @ nginx_back ~] # Grep "grep" test. log
2. alias grep = 'grep -- color = auto'. The lecture is based on grep.
Linux regular expression. It is mainly the regular expression of the three muskeys of awk, sed, and grep (egrep.
A. Basic Regular Expression (based on grep)
[Root @ nginx_back ~] # Grep ". $" test. log
Linux regular expression:
In short, regular expressions are a set of rules and methods defined to process a large number of strings. For example, suppose @ represents 123456 ,! Represents abcde. the system administrator can quickly filter, replace, or output the desired characters by defining these special symbols.
Notes for using regular expressions:
1. linux regular expressions are generally processed in the unit of action.
2. alias grep = 'grep -- color = auto'. The lecture is based on grep.
3. Note the character set LC_ALL = C.
Linux regular expression. It is mainly the regular expression of the three muskeys of awk, sed, and grep (egrep.
A. Basic Regular Expression (based on grep)
1. ^ a indicates searching for content starting with
2. $ a indicates searching for content ending with
3. ^ $ indicates empty rows, not Spaces
4.
[Root @ nginx_back ~] # Grep "\. $" test. log
4.
[Root @ nginx_back ~] # Grep "\. * $" test. log
Linux regular expression:
In short, regular expressions are a set of rules and methods defined to process a large number of strings. For example, suppose @ represents 123456 ,! Represents abcde. the system administrator can quickly filter, replace, or output the desired characters by defining these special symbols.
Notes for using regular expressions:
1. linux regular expressions are generally processed in the unit of action.
2. alias grep = 'grep -- color = auto'. The lecture is based on grep.
3. Note the character set LC_ALL = C.
[Root @ nginx_back ~] # Grep ". *" test. log
Linux regular expression:
In short, regular expressions are a set of rules and methods defined to process a large number of strings. For example, suppose @ represents 123456 ,! Represents abcde. the system administrator can quickly filter, replace, or output the desired characters by defining these special symbols.
Notes for using regular expressions:
1. linux regular expressions are generally processed in the unit of action.
2. alias grep = 'grep -- color = auto'. The lecture is based on grep.
3. Note the character set LC_ALL = C.
Linux regular expression. It is mainly the regular expression of the three muskeys of awk, sed, and grep (egrep.
A. Basic Regular Expression (based on grep)
1. ^ a indicates searching for content starting with
2. $ a indicates searching for content ending with
3. ^ $ indicates empty rows, not Spaces
4.
[Root @ nginx_back ~] # Grep "." test. log
Linux regular expression:
In short, regular expressions are a set of rules and methods defined to process a large number of strings. For example, suppose @ represents 123456 ,! Represents abcde. the system administrator can quickly filter, replace, or output the desired characters by defining these special symbols.
Notes for using regular expressions:
1. linux regular expressions are generally processed in the unit of action.
2. alias grep = 'grep -- color = auto'. The lecture is based on grep.
3. Note the character set LC_ALL = C.
Linux regular expression. It is mainly the regular expression of the three muskeys of awk, sed, and grep (egrep.
A. Basic Regular Expression (based on grep)
1. ^ a indicates searching for content starting with
2. $ a indicates searching for content ending with
3. ^ $ indicates empty rows, not Spaces
4.
[Root @ nginx_back ~] # Grep ". $" test. log
Linux regular expression:
In short, regular expressions are a set of rules and methods defined to process a large number of strings. For example, suppose @ represents 123456 ,! Represents abcde. the system administrator can quickly filter, replace, or output the desired characters by defining these special symbols.
Notes for using regular expressions:
1. linux regular expressions are generally processed in the unit of action.
2. alias grep = 'grep -- color = auto'. The lecture is based on grep.
3. Note the character set LC_ALL = C.
Linux regular expression. It is mainly the regular expression of the three muskeys of awk, sed, and grep (egrep.
A. Basic Regular Expression (based on grep)
1. ^ a indicates searching for content starting with
2. $ a indicates searching for content ending with
3. ^ $ indicates empty rows, not Spaces
4.
[Root @ nginx_back ~] # Grep ". * $" test. log
Linux regular expression:
In short, regular expressions are a set of rules and methods defined to process a large number of strings. For example, suppose @ represents 123456 ,! Represents abcde. the system administrator can quickly filter, replace, or output the desired characters by defining these special symbols.
Notes for using regular expressions:
1. linux regular expressions are generally processed in the unit of action.
2. alias grep = 'grep -- color = auto'. The lecture is based on grep.
3. Note the character set LC_ALL = C.
Linux regular expression. It is mainly the regular expression of the three muskeys of awk, sed, and grep (egrep.
A. Basic Regular Expression (based on grep)
1. ^ a indicates searching for content starting with
2. $ a indicates searching for content ending with
3. ^ $ indicates empty rows, not Spaces
4.
[Root @ nginx_back ~] # Grep "\. *" test. log
Linux regular expression:
In short, regular expressions are a set of rules and methods defined to process a large number of strings. For example, suppose @ represents 123456 ,! Represents abcde. the system administrator can quickly filter, replace, or output the desired characters by defining these special symbols.
Notes for using regular expressions:
1. linux regular expressions are generally processed in the unit of action.
2. alias grep = 'grep -- color = auto'. The lecture is based on grep.
3. Note the character set LC_ALL = C.
Linux regular expression. It is mainly the regular expression of the three muskeys of awk, sed, and grep (egrep.
A. Basic Regular Expression (based on grep)
1. ^ a indicates searching for content starting with
2. $ a indicates searching for content ending with
3. ^ $ indicates empty rows, not Spaces
4.
[Root @ nginx_back ~] # Grep "grep" test. log
2. alias grep = 'grep -- color = auto'. The lecture is based on grep.
Linux regular expression. It is mainly the regular expression of the three muskeys of awk, sed, and grep (egrep.
A. Basic Regular Expression (based on grep)
[Root @ nginx_back ~] # Grep "\." test. log
In short, regular expressions are a set of rules and methods defined to process a large number of strings. For example, suppose @ represents 123456 ,! Represents abcde. the system administrator can quickly filter, replace, or output the desired characters by defining these special symbols.
1. linux regular expressions are generally processed in the unit of action.
2. alias grep = 'grep -- color = auto'. The lecture is based on grep.
3. Note the character set LC_ALL = C.
1. ^ a indicates searching for content starting with
2. $ a indicates searching for content ending with
3. ^ $ indicates empty rows, not Spaces
4.
[Root @ nginx_back ~] # Grep "C $" test. log
3. Note the character set LC_ALL = C.
[Root @ nginx_back ~] # Grep-v-n "^ $" test. log
1: linux regular expression:
2: in short, regular expressions are a set of rules and methods defined to process a large number of strings. For example, suppose @ represents 123456 ,! Represents abcde. the system administrator can quickly filter, replace, or output the desired characters by defining these special symbols.
3: Notes for using regular expressions:
. Linux regular expressions are generally processed in the unit of action.
. Alias grep = 'grep -- color = auto'. The lecture is based on grep.
. Pay attention to the character set, LC_ALL = C
7: linux regular expression. It is mainly the regular expression of the three muskeys of awk, sed, and grep (egrep.
10: a. Basic Regular Expression (based on grep)
11: 1. ^ a indicates searching for content starting with
12: 2. $ a indicates searching for content ending with
13: 3. ^ $ indicates empty rows, not Spaces
14: 4.
[Root @ nginx_back ~] # Grep "^ linux" test. log
Linux regular expression:
Linux regular expression. It is mainly the regular expression of the three muskeys of awk, sed, and grep (egrep. ,
[Root @ nginx_back ~] # Cat test. log
[Aaa]
Cfg1 = aaa
Xxxx = bbb
Cfg2 = ccc
Cfg3 = ddd
[Bbb]
Cfg1 = eee
Yyyy = fff
Cfg2 = ggg
Cfg3 = hhh
Cfg4 = iii
[Ccc]
Cfg1 = jjj
Zzzz = kkk
Cfg2 = lll
Cfg2 = mmm
Cfg2 = nnn
[Root @ nginx_back ~] # Grep "c *" test. log
[Aaa]
Cfg1 = aaa
Xxxx = bbb
Cfg2 = ccc
Cfg3 = ddd
[Bbb]
Cfg1 = eee
Yyyy = fff
Cfg2 = ggg
Cfg3 = hhh
Cfg4 = iii
[Ccc]
Cfg1 = jjj
Zzzz = kkk
Cfg2 = lll
Cfg2 = mmm
Cfg2 = nnn
[Root @ nginx_back ~] # Grep-o "c *" test. log
C
C
Ccc
C
C
C
C
C
Ccc
C
C
C
C
[Root @ nginx_back ~] # Grep-o ". *" test. log
[Aaa]
Cfg1 = aaa
Xxxx = bbb
Cfg2 = ccc
Cfg3 = ddd
[Bbb]
Cfg1 = eee
Yyyy = fff
Cfg2 = ggg
Cfg3 = hhh
Cfg4 = iii
[Ccc]
Cfg1 = jjj
Zzzz = kkk
Cfg2 = lll
Cfg2 = mmm
Cfg2 = nnn
[Root @ nginx_back ~] # Alias grep = 'grep -- color = auto'
[Root @ nginx_back ~] # Grep "c *" test. log [aaa]
Cfg1 = aaa
Xxxx = bbb
Cfg2 = ccc
Cfg3 = ddd
[Bbb]
Cfg1 = eee
Yyyy = fff
Cfg2 = ggg
Cfg3 = hhh
Cfg4 = iii
[Ccc]
Cfg1 = jjj
Zzzz = kkk
Cfg2 = lll
Cfg2 = mmm
Cfg2 = nnn
[Root @ nginx_back ~] # Grep-o "c *" test. log
C
C
Ccc
C
C
C
C
C
Ccc
C
C
C
C
[Root @ nginx_back ~] # Grep-o ". *" test. log
[Aaa]
Cfg1 = aaa
Xxxx = bbb
Cfg2 = ccc
Cfg3 = ddd
[Bbb]
Cfg1 = eee
Yyyy = fff
Cfg2 = ggg
Cfg3 = hhh
Cfg4 = iii
[Ccc]
Cfg1 = jjj
Zzzz = kkk
Cfg2 = lll
Cfg2 = mmm
Cfg2 = nnn
[Root @ nginx_back ~] # Grep "[abc]" test. log
[Aaa]
Cfg1 = aaa
Xxxx = bbb
Cfg2 = ccc
Cfg3 = ddd
[Bbb]
Cfg1 = eee
Cfg2 = ggg
Cfg3 = hhh
Cfg4 = iii
[Ccc]
Cfg1 = jjj
Cfg2 = lll
Cfg2 = mmm
Cfg2 = nnn
[Root @ nginx_back ~] # Grep "[a-z]" test. log
[Aaa]
Cfg1 = aaa
Xxxx = bbb
Cfg2 = ccc
Cfg3 = ddd
[Bbb]
Cfg1 = eee
Yyyy = fff
Cfg2 = ggg
Cfg3 = hhh
Cfg4 = iii
[Ccc]
Cfg1 = jjj
Zzzz = kkk
Cfg2 = lll
Cfg2 = mmm
Cfg2 = nnn
[Root @ nginx_back ~] # Grep "[0-9]" test. log
Cfg1 = aaa
Cfg2 = ccc
Cfg3 = ddd
Cfg1 = eee
Cfg2 = ggg
Cfg3 = hhh
Cfg4 = iii
Cfg1 = jjj
Cfg2 = lll
Cfg2 = mmm
Cfg2 = nnn
[Root @ nginx_back ~] # Vi test. log
[Aaa]
Cfg1 = aaa
. Xxxx = bbb
Cfg2 = ccc
. Cfg3 = ddd
[Bbb]
Cfg1 = eee
. Yyyy = fff/
Cfg2 = ggg
Cfg3 = hhh
C. fg4 = iii/
[Ccc]
Cf. g1 = jjj
Zzzz = kkk/
Limit .2 = lll.
"Test. log" 17L, 154C written
[Root @ nginx_back ~] # Grep "[\.,/]" test. log
. Xxxx = bbb
. Cfg3 = ddd
. Yyyy = fff/
C. fg4 = iii/
Cf. g1 = jjj
Zzzz = kkk/
Limit .2 = lll.
[Root @ nginx_back ~] # Grep "[^ abc]" test. log
[Aaa]
Cfg1 = aaa
. Xxxx = bbb
Cfg2 = ccc
. Cfg3 = ddd
[Bbb]
Cfg1 = eee
. Yyyy = fff/
Cfg2 = ggg
Cfg3 = hhh
C. fg4 = iii/
[Ccc]
Cf. g1 = jjj
Zzzz = kkk/
Limit .2 = lll.
Cfg2 = mmm
Cfg2 = nnn
[Root @ nginx_back ~] # Vi test. log
[Aaa]
Cfg1 = aaa00000
0. xxxx = bbb
Cfg2 = ccc
. 00cfg3 = ddd
[Bbb] 0000
Cfg1 = eee
. Yy00yy = fff/
Cfg2 = ggg
Cfg3 = hhh00000
C. fg400 = iii/
[Ccc]
Cf. g1 = jjj
Zzzz = kk000000000k/
Limit .2 = lll.
Cfg2 = mmm
Cfg2 = nnn
"Test. log" 17L, 184C written
[Root @ nginx_back ~] # Grep "0 \ {1, 3 \}" test. log
Cfg1 = aaa00000
0. xxxx = bbb
. 00cfg3 = ddd
[Bbb] 0000
. Yy00yy = fff/
Cfg3 = hhh00000
C. fg400 = iii/
Zzzz = kk000000000k/
[Root @ nginx_back ~] # Grep "0 \ {1, \}" test. log
Cfg1 = aaa00000
0. xxxx = bbb
. 00cfg3 = ddd
[Bbb] 0000
. Yy00yy = fff/
Cfg3 = hhh00000
C. fg400 = iii/
Zzzz = kk000000000k/
[Root @ nginx_back ~] # Grep "0 \ {2 \}" test. log
Cfg1 = aaa00000
. 00cfg3 = ddd
[Bbb] 0000
. Yy00yy = fff/
Cfg3 = hhh00000
C. fg400 = iii/
Zzzz = kk000000000k/
[Root @ nginx_back ~] # Egrep "0 {1, 3}" test. log
Cfg1 = aaa00000
0. xxxx = bbb
. 00cfg3 = ddd
[Bbb] 0000
. Yy00yy = fff/
Cfg3 = hhh00000
C. fg400 = iii/
Zzzz = kk000000000k/
Common grep parameters:
-A: searches for data in binary files as text files.
-C: calculates the number of times a 'search string' is found.
-O: displays only the content that matches the regexp (used to count the number of times in the text)
-I: Case sensitivity is ignored, so the case sensitivity is the same.
-N: displays the row number at the beginning of the row.
-V: reverse selection, that is, the row without the 'search string' content is displayed.
-E: Extended grep, that is, egrep
-- Color = auto: highlight matching keywords in a specific color
# <-Tip:-I-v is a common Parameter
-A: After indicates that data in n rows After matching strings is displayed.
-B: before indicates that the data of the first n rows matching the string is displayed.
The online environment example cases will be sorted in the future: