Get the App
SLTechnology News&Howtos  ›  Servers  › 

Shell programming the grep of the three Musketeers

Shulou Source: shulou.com Published: 2022-06-02 22:37:59 10月01日 Update

Regular expression

Definition of regular expressions:

Regular expressions are also known as regular expressions and regular expressions. It is often abbreviated to regex, regexp, or RE in code. A regular expression uses a single string to describe and match a series of strings that conform to certain syntactic rules. To put it simply, it is a method of matching strings, through some special symbols, to quickly find, delete, and replace a specific string.

Basic regular expression

Take the httpd configuration file as an example

In order to prevent the error from damaging the httpd configuration file, let's first copy a copy of the httpd configuration file elsewhere.

1), find specific characters

The location of the specific character "the" can be found in the httpd.txt file.

You can use the command: grep-n 'the' httpd.txt

Find lines that do not contain the character "the"

You can use the command grep-vn 'the' httpd.txt

2) use brackets "[]" to find collection characters

When you look for the strings "then" and "they", you can find that both strings contain "the". Execute the following command at this point: grep-n 'the [ny]' httpd.txt

You can find both "then" and "they" strings. No matter how many characters there are in "[]", they all represent only one character, that is, "[ny]" means to match "n" or "y".

When looking for a duplicate single character "oo"

You can use the command: grep-n 'oo' httpd.txt

Look for a string that is not preceded by "r" before "oo"

You can use the command: grep-n'[^ r] oo' httpd.txt

If you don't want "oo" preceded by uppercase letters, you can use the "grep-n'[^ Amurz] oo'httpd.txt" command to do so.

Finding rows that contain numbers can be done with the "grep-n'[0-9] 'httpd.txt" command

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

Query the exercise at the beginning of a capital letter with grep-n'^ [Amurz] 'httpd.txt

Query "." The exercise at the end is grep-n'. $'httpd.txt

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.

When querying blank lines, execute the "grep-n'^ $'httpd.txt" command

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

In a regular expression, the decimal point (.) is also a metacharacter that represents any character. For example, to find a string that begins with r and ends with t, with two unknown characters in the middle, you can use the command grep-n'r.. t 'httpd.txt.

If the query contains at least two or more strings, execute the "grep-n 'ooo*' httpd.txt" command.

5) find the continuous character range "{}"

Because "{}" has a special meaning in Shell, when using the "{}" character, we need to use the escape character "\" to convert the "{}" character into a normal character.

(1) query the characters of two o

Grep-n'o\ {2\} 'httpd.txt

Tags: Character command string expression regular two query file configuration normal special representative metacharacter single uppercase uppercase letter beginning meaning escape Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno vpn Microsoft OPPO Reno macOS Shulou Information