shell programming of the regular expression (a) basic regular expressions

Regular expressions
before learning the basic usage of Shell scripts, can already make use of conditional, recycling and other statements Shell script editor. Next, we will begin to introduce a very important concept - the regular expression (RegularExpression, RE).
Regular expressions define
regular expressions, also known as regular expressions, regular expressions. Code often abbreviated as regex, regexp or RE. Regular expressions are used to describe the use of a single string, the string line with a series of matching syntax rules, in simple terms, is a way to match the string, through some special symbols, quick search, delete, replace a specific string.
Regular expressions are text mode consists of common characters and meta characters. Model is used to describe one or more of the text string to match when searching. Regular expression as a template, a character pattern to match with the search string. Where ordinary characters including uppercase and lowercase letters, numbers, punctuation marks and other symbols, meta characters are those characters having a specific meaning in particular regular expression, which can be used to specify the leading character (i.e., the preceding character is located meta character ) appear in the target object.
Regular expressions are generally used for programming and scripting text editor. Many text processor and programming languages support regular expressions, Perl as previously mentioned, Linux system common text processors (grep, egrep, sed, awk ). Regular expressions have a very powerful text-matching capabilities to quickly and efficiently handle text in a text sea.
Regular expressions use
for the average computer user, because few opportunities to use regular expressions, so I can not understand the charm of the regular expression, and for system administrators, the regular expression is one of the necessary skills.
Regular Expressions for system administrators is very important, the system is running will generate a lot of information that is important to some, while others are only informed of the information. As an administrator if the direct data to see so much information, not quickly locate important information, such as "user account login failed" and "service failed to start" and other information. Then you can quickly extract information expressions "problematic" by the positive. As a result, operation and maintenance work can become more simple and convenient.
At present, many software also supports regular expressions, the most common is the mail server. On the Internet, junk / spam often cause network congestion, if the server will reject these issues in advance of the mail, then the client will reduce a lot of unnecessary bandwidth consumption. The most commonly used mail server postfix mail server support and related software supports regular expression matching. The title and content of the letter with the special string comparison, find problem-mail to filter out.
In addition to the mail server, a lot of server software supports regular expressions. Although these software supports regular expressions, but the string comparison rules also require the system administrator to add, so as a system administrator, the regular expression is one of the skills must be mastered.
Basis regular expression
string expression methods into a regular expression with the regular expression substantially extended regular expression depending on the degree of stringency function. Basic regular expressions are commonly used in some of the most basic of regular expressions. In the Linux system common file processing tool grep and sed to support basic regular expressions and egrep and awk supports extended regular expressions. Master the basic regular expressions using the method, we must first understand the basic meaning of the regular expression metacharacters included, the following grep command by way of example introduced one by one.
Basic regular expressions Example:
Find a specific character is very simple, as you can execute the following command from test.txt file to find out the specific character "the" location. Wherein "-n" denotes a display line number, "- i" represents a case insensitive. After the command is executed, character meet the matching criteria, font color turns red (chapter all bold instead of through).
Find specific character

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

shell programming of the regular expression (a) basic regular expressions

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

shell programming of the regular expression (a) basic regular expressions
If adverse selection, such as finding does not contain "the" character line, it needs to be achieved by "-vn" option grep command.

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

shell programming of the regular expression (a) basic regular expressions
The use of brackets "[]" to find a collection of characters
when you want to find "shirt" and "short" two strings can be found in the two strings contains "sh" and "rt". Run the following command to simultaneously find "shirt" and "short" two strings. "[]" In regardless of the number of characters, are only representative of a character, that "[io]" means match "i" or "o".

[root@localhost ~]# grep -n 'sh[io]rt' test.txt

shell programming of the regular expression (a) basic regular expressions
When To find duplicate contains a single character "oo", just execute the following command.

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

shell programming of the regular expression (a) basic regular expressions
If the Find "OO" not preceded by a "w" of the character string, only the character set by selecting a reverse "[^]" This object is achieved, such as the implementation of "grep -n '[^ w] oo'test.txt" Find command represents "oo" not preceded by "w" in the string of text test.txt.

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

shell programming of the regular expression (a) basic regular expressions
Found "woood" and "wooooood" matching rule is also consistent with the execution result of the command, both containing "w". In fact, it can be seen from the results of the matching criteria in line with bold characters, and these results may be that, "# woood #" is shown in bold in the "ooo", and "oo" in front of "o" the match rule. Similarly "#woooooood #" is also consistent with the matching rule.
If desired "oo" exists in front of lower case letters may be used "grep -n '[^ az] oo'test.txt" command is implemented, wherein "az" represents lowercase letters, uppercase letters through "AZ" FIG.

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

shell programming of the regular expression (a) basic regular expressions

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

It was filtered to az, AZ string that begins
the row containing numbers look may be achieved by "grep -n '[0-9]' test.txt" command.

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

shell programming of the regular expression (a) basic regular expressions
Find the line song "^" and end of line characters "$"
foundation regular expression contains two positioning metacharacters: "^" (first line) and "$" (end of line). In the above example, the query "the" there have been a string that contains a lot of "the" in-line, if you want to check in "the" beginning of a line string line, you can use the "^" metacharacter to achieve.

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

shell programming of the regular expression (a) basic regular expressions

[root@localhost ~]# grep -n '\.$‘’ test.txt

shell programming of the regular expression (a) basic regular expressions
当查询空白行时,执行“grep –n‘^$’test.txt”命令即可。

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

shell programming of the regular expression (a) basic regular expressions
查找任意一个字符“.”与重复字符“*”

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

shell programming of the regular expression (a) basic regular expressions
”代表的是重复零个或多个前面的单字符。“o”表示拥有零个(即为空字符)或大于等于一个“o”的字符,因为允许空字符,所以执行“grep –n‘o’test.txt”命令会将文本中所有的内容都输出打印。如果是“oo”, 则第一个 o 必须存在,第二个 o 则是零个或多个 o,所以凡是包含 o、oo、ooo、ooo,等的资料都符合标准。同理,若查询包含至少两个 o 以上的字符串,则执行“grep –n‘ooo*’ test.txt”命令即可。

[root@localhost ~]# grep -n 'ooo*' test.txt

shell programming of the regular expression (a) basic regular expressions
查询以 w 开头 d 结尾,中间包含至少一个 o 的字符串,执行以下命令即可实现。

[root@localhost ~]# grep -n 'woo*d' test.txt

shell programming of the regular expression (a) basic regular expressions
查询以 w 开头 d 结尾,中间的字符可有可无的字符串。

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

shell programming of the regular expression (a) basic regular expressions
查询任意数字所在行

[root@localhost ~]# grep -n '[0-9][0-9]*' test.txt

shell programming of the regular expression (a) basic regular expressions
查找连续字符范围“{}”
在上面的示例中,我们使用“.”与“*”来设定零个到无限多个重复的字符,如果想要限制一个范围内的重复的字符串该如何实现呢?例如,查找三到五个 o 的连续字符,这个时候就需要使用基础正则表达式中的限定范围的字符“{}”。因为“{}”在 Shell 中具有特殊 意义,所以在使用“{}”字符时,需要利用转义字符“\”,将“{}”字符转换成普通字符。 “{}”字符的使用方法如下所示。
查询两个 o 的字符:

[root@localhost ~]# grep -n 'o\{2\}' test.txt

shell programming of the regular expression (a) basic regular expressions
查询以 w 开头以 d 结尾,中间包含 2~5 个 o 的字符串

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

shell programming of the regular expression (a) basic regular expressions
查询以 w 开头以 d 结尾,中间包含 2 以上 o 的字符串

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

shell programming of the regular expression (a) basic regular expressions
Metacharacter summary
shell programming of the regular expression (a) basic regular expressions
extended regular expressions
usually use basic regular expressions would have been sufficient, but sometimes in order to simplify the instructions, require the use of a wider range of extended regular expressions. For example, using basic regular expressions query "grep -v '^ $' test.txt file in addition to the first blank row to row as a" # "outside line (usually used to view the configuration file into effect), the Executive | grep - v '^ #' "can be realized. You should use the pipe command to search twice. If extended regular expressions can be simplified as "egrep -v '^ $ | ^ #' test.txt", wherein the inner pipe symbol represents a single or a quote (or).
In addition, grep command supports only basic regular expressions, if you use extended regular expressions, use egrep or awk commands. awk commands explained in the following sections, here we directly use the egrep command. egrep command grep command usage is similar. egrep command is a file search mode is obtained, can use this command string and any symbol file search, the search string may be one or more files, a prompt may be a single character, a string, a word or A sentence.
Regular basis with the same type of expression, extended regular expressions can also contain more meta characters, extended regular expression common
type of meta-characters include the following
shell programming of the regular expression (a) basic regular expressions

Guess you like

Origin blog.51cto.com/14557905/2455949