Of being a shell script expressions

First, the basic regular expression instance:
metacharacter Summary:
Of being a shell script expressions
common in the Linux file system processing tools grep and sed to support basic regular expressions.

grep command options:

                        -i:查找时不区分大小写;
                        -v:查找时反向输出,如查找不包含某些字符的内容;
                        -n:表示查找出结果后显示行号;

These three options can be used in combination, such as "-in", not case-sensitive search and display line numbers.
Example ①:

[root@localhost ~]# grep -n 'the' test.txt                   #查找test文件中包含字符“the”的行
#可以将选项改为“-vn”来查找不包含“the”的行。

Example ②:

[root@localhost ~]# grep -n "sh[io]rt" test.txt                   #[io]表示匹配 i 或o的显示出来
#[ ]中无论有几个字符都仅代表匹配一个字符即可。

Example ③:

[root@localhost ~]# grep -n 'oo' test.txt                 #查找包含字符“oo”的行。
[root@localhost ~]# grep -n 'ooo*' test.txt              #查找包含至少两个o以上的字符串。
[root@localhost ~]# grep -n 'o\{2\}' test.txt             #查找包含两个“o”的字符串。
 [root@localhost ~]# grep -n 'o\{2,5\}' test.txt          #查找包含2~5个o的字符串。
 [root@localhost ~]# grep -n 'o\{2,\}' test.txt             #查找包含两个以上“o”的字符串。

Example ④:

[root@localhost ~]# grep -n '[^w]oo' test.txt           #查找“oo”前面不是w的字符串。
[root@localhost ~]# grep -n '[^a-z]oo' test.txt          #查找oo前不是小写字母的行。
[root@localhost ~]# grep -n '[0-9]' test.txt               #查找包含数字的行。
[root@localhost ~]# grep -n '^the' test.txt             #查找以“the”开头的行。
[root@localhost ~]# grep -n '^[a-z]' test.txt             #查找以小写字母开头的行。
[root@localhost ~]# grep -n '^[A-Z]' test.txt            #查找以大写字母开头的行。 
[root@localhost ~]# grep -n '^[^a-zA-Z]' test.txt       #查找不以字母开头的行。
#     “^”在[ ] 号外面表示定位行首,也就是以某些内容开头,若在[ ]内则表示反向选择。
[root@localhost ~]# grep -n '\.$' test.txt             #查找以 “ .  ” 结尾的行。
[root@localhost ~]# grep -n 'w..d' test.txt             #查找w开头,中间两个未知字符,d结尾的行。
[root@localhost ~]# grep -n 'woo*d' test.txt     #查找w开头d结尾,中间至少包含一个o的字符串。
[root@localhost ~]# grep -n 'w.*d' test.txt        #查找w开头d结尾,中间的字符可有可无的字符串。
[root@localhost ~]# grep -n '[0-9][0-9]*' test.txt            #查询任意数字所在行

Second, the extended regular expression
generally sufficient basis for regular expressions we use, but if you want to simplify the instructions, then you can use extended regular expressions, if you use extended regular expressions, egrep or awk commands need to use common the extended regular expression metacharacters mainly include the following:
Of being a shell script expressions

Guess you like

Origin blog.51cto.com/14154700/2400958