I. Basic Regular Expression example:
Metacharacter summary:
In the common file processing tools in Linux system, grep and sed support basic regular expressions.
grep command options:
-i: case insensitive when searching; -v: reverse output when searching, such as searching for content without some characters; -n: display the line number after finding the result;
These three options can be used together, such as "- in", which is case insensitive and displays the line number.
Examples:
[root@localhost ~]# grep -n 'the' test.txt #Find the line in the test file that contains the character "the" #You can change the option to "- vn" to find rows that do not contain "the".
Examples:
[root@localhost ~]# grep -n "sh[io]rt" test.txt #[i O] indicates the display of matching i or o #No matter how many characters in [], only one character can be matched.
Example:
[root@localhost ~]# grep -n 'oo' test.txt #Find the line containing the character "oo". [root@localhost ~]# grep -n 'ooo*' test.txt #Finds a string containing at least two o's. [root@localhost ~]# grep -n 'o\{2\}' test.txt #Looks up a string containing two 'OS'. [root@localhost ~]# grep -n 'o\{2,5\}' test.txt #Looks up a string containing 2 to 5 o's. [root@localhost ~]# grep -n 'o\{2,\}' test.txt #Looks up a string containing more than two 'o'.
Example 4:
[root@localhost ~]# grep -n '[^w]oo' test.txt #Find a string that does not precede "oo" with a w. [root@localhost ~]# grep -n '[^a-z]oo' test.txt #Find lines that are not lowercase before oo. [root@localhost ~]# grep -n '[0-9]' test.txt #Find the row that contains the number. [root@localhost ~]# grep -n '^the' test.txt #Find lines that start with the. [root@localhost ~]# grep -n '^[a-z]' test.txt #Find rows that start with lowercase letters. [root@localhost ~]# grep -n '^[A-Z]' test.txt #Find lines that start with uppercase letters. [root@localhost ~]# grep -n '^[^a-zA-Z]' test.txt #Find lines that do not start with a letter. # "^" outside the [] indicates the beginning of the positioning line, that is to say, it starts with some content. If it is inside the [] indicates reverse selection. [root@localhost ~]# grep -n '\.$' test.txt #Find rows ending with ".". [root@localhost ~]# grep -n 'w..d' test.txt #Look for lines beginning with w, two unknown characters in the middle, and ending with d. [root@localhost ~]# grep -n 'woo*d' test.txt #Find a string that begins w and ends d with at least one o in the middle. [root@localhost ~]# grep -n 'w.*d' test.txt #Look for a string that starts with w and ends with d. the characters in the middle can be optional. [root@localhost ~]# grep -n '[0-9][0-9]*' test.txt #Query the line of any number
2. Extended regular expression
Generally speaking, the basic regular expression is enough for us to use, but if we want to simplify the whole instruction, we can use the extended regular expression. If we use the extended regular expression, we need to use the egrep or awk command. The common metacharacters of the extended regular expression mainly include the following: