Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Three Musketeers of regular expressions in Shell programming-- grep,egrep

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/02 Report--

Overview of regular expressions

A regular expression is a logical formula for manipulating a string, that is, a "regular string" is formed by predefined specific characters and a combination of these specific characters. This "regular string" is used to express a filtering logic for a string.

Basic regular expression

The string expression method of regular expression can be divided into basic regular expression and extended regular expression according to different degree of rigor and function. The underlying regular expression is the most basic part of a commonly used regular expression. In the common file processing tools in Linux systems, grep and sed support basic regular expressions, while egrep and awk support extended regular expressions.

The underlying regular expression example takes the configuration file of the httpd service as an example (/ etc/httpd/conf/httpd.conf) 1) find a specific character-n: display line number-I: case-insensitive find the character "the" in the target file [root@localhost conf] # grep-n 'the' httpd.conf

If you reverse search and find lines that do not contain the character "the", you need to implement [root@localhost conf] # grep-vn 'the' httpd.conf through the "- vn" option.

2) use brackets "[]" to find collection characters (first create a custom text file a.txt)

When you look for the strings "shirt" and "short", you can find that both strings contain "sh" and "rt". At this point, execute the following command to find both "shirt" and "short". No matter how many characters there are in "[]", they represent only one character, that is, "[io]" matches "I" or "o".

# a.txt experiment text shortshirtootoolswoodwooooooodWoodthe footballthis the web123123456456. [root@localhost ~] # grep-n'shio] rt' a.txt

To find a duplicate single character "oo", simply execute the following command to [root@localhost ~] # grep-n 'oo' a.txt

If you look for a string that is not preceded by "w" before "oo", you only need to do this by selecting "[^]" in the reverse direction of the collection characters.

[root@localhost ~] # grep-n'[^ w] oo' a.txt

In the execution result of the above command, it is found that "wooooood" also matches the matching rule, including "w". In fact, from the execution results, we can see that the characters that meet the matching criteria are shown in bold, and in the above results, we can see that the bold display in "# wooooood #" is "oooooo", and the "o" before "oo" is in line with the matching rules.

If you do not want the lowercase letter "oo" to be preceded by the lowercase letter Amurz Amurz number 0-9 [root@localhost ~] # grep-n'[^ aMuz] oo' a.txt

3) find the beginning of the line "^" and the character "$" at the end of the line

The underlying regular expression contains two positioning metacharacters: "^" (the beginning of the line) and "$" (the end of the line). In the above example, there are many lines containing "the" when querying the "the" string, and if you want to query lines that begin with the "the" string, you can do so with the "^" metacharacter.

[root@localhost ~] # grep-n'^ the' test.txt

Queries that begin with lowercase letters can be filtered by the "^ [Amurz]" rule, lines that begin with uppercase letters can be filtered using "^ [Amurz]", and queries that do not begin with letters use the "^ [^ a-zA-Z]" rule.

[root@localhost ~] # grep-n'^ [^ a-zA-Z] 'a.txt

The function of the "^" symbol is different inside and outside the metacharacter set "[]" symbol, indicating reverse selection within the "[]" symbol and positioning the beginning of the line outside the "[]" symbol. Conversely, you can use the "$" locator if you want to find a line that ends with a particular character. For example, execute the following command to query rows that end with a decimal point (.). Because the decimal point (.) is also a metacharacter in regular expressions (which will be discussed later), you need to use the escape character "\" to convert characters with special meaning into ordinary characters. Blank lines can be used with ^ $.

[root@localhost ~] # grep-n'\. $'a.txt

4) find any character "." And the repeating character "*"

As mentioned earlier, the decimal point (.) in a regular expression is also a metacharacter that represents any character. For example, execute the following command to find a string of four characters that begins with w and ends with d.

In the above results, the "wood" string "w... d" matches the rule. If you want to query oo, ooo, ooooo, and so on, you need to use the asterisk () metacharacter. It is important to note, however, that "" represents the repetition of zero or more of the first single characters.

[root@localhost ~] # grep-n 'oo*' a.txt

The query begins with a w and ends with a dispensable string of characters in the middle.

[root@localhost ~] # grep-n 'w.d' a.txt

Query the row of any number.

[root@localhost] # grep-n'[0-9] [0-9] 'a.txt

5) find continuous character range "{}"

The query begins with w and ends with d, with a string of 2'5 o in the middle.

[root@localhost ~] # grep-n'wo {2pm 5} d 'a.txt

The query begins with w and ends with d, with more than 2 o strings in the middle.

[root@localhost ~] # grep-n'wo {2,} d 'a.txt

Metacharacters summarize common basic regular expressions the metacharacters of regular expressions mainly include the following

Extended regular expression

In general, the use of basic regular expressions is sufficient, but sometimes a wider range of extended regular expressions is needed to simplify the entire instruction.

The grep command only supports basic regular expressions, and if you use extended regular expressions, you need to use the egrep or awk command.

Common metacharacters of extended regular expressions

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report