grep (global search regular expression(RE) and print out the line,全面搜索正则表达式并把行打印出来)是一种强大的文本搜索工具,它能使用正则表达式搜索文本,并把匹配的行打印出来。正则表达式
字符类的搜索:若是我想要搜寻 test 或 taste 这两个单字时,能够发现到,其实她们有共通的 't?st' 存在~这个时候,我能够这样来搜寻:shell
[root@www ~]# grep -n 't[ae]st' regular_express.txt 8:I can't finish the test. 9:Oh! The soup taste good.
其实 [] 里面不论有几个字节,他都谨表明某『一个』字节, 因此,上面的例子说明了,我须要的字串是『tast』或『test』两个字串而已!express
字符类的反向选择 [^] :若是想要搜索到有 oo 的行,但不想要 oo 前面有 g,以下app
[root@www ~]# grep -n '[^g]oo' regular_express.txt 2:apple is my favorite food. 3:Football game is not use feet only. 18:google is the best tools for search keyword. 19:goooooogle yes!
第 2,3 行没有疑问,由于 foo 与 Foo 都可被接受!工具
可是第 18 行明明有 google 的 goo 啊~别忘记了,由于该行后面出现了 tool 的 too 啊!因此该行也被列出来~ 也就是说, 18 行里面虽然出现了咱们所不要的项目 (goo) 可是由於有须要的项目 (too) , 所以,是符合字串搜寻的喔!this
至于第 19 行,一样的,由于 goooooogle 里面的 oo 前面多是 o ,例如: go(ooo)oogle ,因此,这一行也是符合需求的!google
字符类的连续:再来,假设我 oo 前面不想要有小写字节,因此,我能够这样写 [^abcd....z]oo , 可是这样彷佛不怎么方便,由於小写字节的 ASCII 上编码的顺序是连续的, 所以,咱们能够将之简化为底下这样:编码
[root@www ~]# grep -n '[^a-z]oo' regular_express.txt 3:Football game is not use feet only.
也就是说,当咱们在一组集合字节中,若是该字节组是连续的,例如大写英文/小写英文/数字等等, 就可使用[a-z],[A-Z],[0-9]等方式来书写,那么若是咱们的要求字串是数字与英文呢? 呵呵!就将他所有写在一块儿,变成:[a-zA-Z0-9]。spa
咱们要取得有数字的那一行,就这样:code
[root@www ~]# grep -n '[0-9]' regular_express.txt 5:However, this dress is about $ 3183 dollars. 15:You are the best is mean you are the no. 1.
行首与行尾字节 ^ $
行首字符:若是我想要让 the 只在行首列出呢? 这个时候就得要使用定位字节了!咱们能够这样作:
[root@www ~]# grep -n '^the' regular_express.txt 12:the symbol '*' is represented as start.
此时,就只剩下第 12 行,由于只有第 12 行的行首是 the 开头啊~此外, 若是我想要开头是小写字节的那一行就列出呢?能够这样:
[root@www ~]# grep -n '^[a-z]' regular_express.txt 2:apple is my favorite food. 4:this dress doesn't fit me. 10:motorcycle is cheap than car. 12:the symbol '*' is represented as start. 18:google is the best tools for search keyword. 19:goooooogle yes! 20:go! go! Let's go.
若是我不想要开头是英文字母,则能够是这样:
[root@www ~]# grep -n '^[^a-zA-Z]' regular_express.txt 1:"Open Source" is a good mechanism to develop programs. 21:# I am VBird
^ 符号,在字符类符号(括号[])以内与以外是不一样的! 在 [] 内表明『反向选择』,在 [] 以外则表明定位在行首的意义!
那若是我想要找出来,行尾结束为小数点 (.) 的那一行:
[root@www ~]# grep -n '\.$' regular_express.txt 1:"Open Source" is a good mechanism to develop programs. 2:apple is my favorite food. 3:Football game is not use feet only. 4:this dress doesn't fit me. 10:motorcycle is cheap than car. 11:This window is clear. 12:the symbol '*' is represented as start. 15:You are the best is mean you are the no. 1. 16:The world <Happy> is the same with "glad". 17:I like dog. 18:google is the best tools for search keyword. 20:go! go! Let's go.
特别注意到,由于小数点具备其余意义(底下会介绍),因此必需要使用转义字符(\)来加以解除其特殊意义!
找出空白行:
[root@www ~]# grep -n '^$' regular_express.txt 22:
由于只有行首跟行尾 (^$),因此,这样就能够找出空白行啦!
任意一个字节 . 与重复字节 *
这两个符号在正则表达式的意义以下:
. (小数点):表明『必定有一个任意字节』的意思; * (星号):表明『重复前一个字符, 0 到无穷屡次』的意思,为组合形态
假设我须要找出 g??d 的字串,亦即共有四个字节, 起头是 g 而结束是 d ,我能够这样作:
[root@www ~]# grep -n 'g..d' regular_express.txt 1:"Open Source" is a good mechanism to develop programs. 9:Oh! The soup taste good. 16:The world <Happy> is the same with "glad".
由于强调 g 与 d 之间必定要存在两个字节,所以,第 13 行的 god 与第 14 行的 gd 就不会被列出来啦!
若是我想要列出有 oo, ooo, oooo 等等的数据, 也就是说,至少要有两个(含) o 以上,该如何是好?
由于 * 表明的是『重复 0 个或多个前面的 RE 字符』的意义, 所以,『o*』表明的是:『拥有空字节或一个 o 以上的字节』,所以,『 grep -n 'o*' regular_express.txt 』将会把全部的数据都列印出来终端上!
当咱们须要『至少两个 o 以上的字串』时,就须要 ooo* ,亦便是:
[root@www ~]# grep -n 'ooo*' regular_express.txt 1:"Open Source" is a good mechanism to develop programs. 2:apple is my favorite food. 3:Football game is not use feet only. 9:Oh! The soup taste good. 18:google is the best tools for search keyword. 19:goooooogle yes!
若是我想要字串开头与结尾都是 g,可是两个 g 之间仅能存在至少一个 o ,亦便是 gog, goog, gooog.... 等等,那该如何?
[root@www ~]# grep -n 'goo*g' regular_express.txt 18:google is the best tools for search keyword. 19:goooooogle yes!
若是我想要找出 g 开头与 g 结尾的行,当中的字符无关紧要
[root@www ~]# grep -n 'g.*g' regular_express.txt 1:"Open Source" is a good mechanism to develop programs. 14:The gd software is a library for drafting programs. 18:google is the best tools for search keyword. 19:goooooogle yes! 20:go! go! Let's go.
由于是表明 g 开头与 g 结尾,中间任意字节都可接受,因此,第 1, 14, 20 行是可接受的喔! 这个 .* 的 RE 表示任意字符是很常见的.
若是我想要找出『任意数字』的行?由于仅有数字,因此就成为:
[root@www ~]# grep -n '[0-9][0-9]*' regular_express.txt 5:However, this dress is about $ 3183 dollars. 15:You are the best is mean you are the no. 1.
限定连续 RE 字符范围 {}
咱们能够利用 . 与 RE 字符及 * 来配置 0 个到无限多个重复字节, 那若是我想要限制一个范围区间内的重复字节数呢?
举例来讲,我想要找出两个到五个 o 的连续字串,该如何做?这时候就得要使用到限定范围的字符 {} 了。 但由于 { 与 } 的符号在 shell 是有特殊意义的,所以, 咱们必需要使用字符 \ 来让他失去特殊意义才行。 至於 {} 的语法是这样的,假设我要找到两个 o 的字串,能够是:
[root@www ~]# grep -n 'o\{2\}' regular_express.txt 1:"Open Source" is a good mechanism to develop programs. 2:apple is my favorite food. 3:Football game is not use feet only. 9:Oh! The soup taste good. 18:google is the best tools for search ke 19:goooooogle yes!
假设咱们要找出 g 后面接 2 到 5 个 o ,而后再接一个 g 的字串,他会是这样:
[root@www ~]# grep -n 'go\{2,5\}g' regular_express.txt 18:google is the best tools for search keyword.
若是我想要的是 2 个 o 以上的 goooo....g 呢?除了能够是 gooo*g ,也能够是:
[root@www ~]# grep -n 'go\{2,\}g' regular_express.txt 18:google is the best tools for search keyword. 19:goooooogle yes!