linux打开car命令 linux cargo( 三 )


[root@www ~]#grep-n'^the' regular_express.txt12:the symbol'*'is represented as start.
此时,就只剩下第 12 行,因为只有第 12 行的行首是 the 开头啊~此外 ,  如果我想要开头是小写字节的那一行就列出呢?可以这样:
[root@www ~]#grep-n'^[a-z]' regular_express.txt2: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 toolsfor search keyword.19:goooooogle yes!20:go! go! Let's go.
如果我不想要开头是英文字母,则可以是这样:
[root@www ~]#grep-n'^[^a-zA-Z]' regular_express.txt1:"Open Source" is a good mechanism to develop programs.21:# I am VBird
^ 符号,在字符类符号(括号[])之内与之外是不同的! 在 [] 内代表『反向选择』,在 [] 之外则代表定位在行首的意义!
那如果我想要找出来,行尾结束为小数点 (.) 的那一行:
[root@www ~]#grep-n'\.$' regular_express.txt1:"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 isclear.12:the symbol'*' is represented as start.15:You are the best is mean you are the no.1.16:The worldis the same with"glad".17:I like dog.18:google is the best toolsfor search keyword.20:go! go! Let's go.
特别注意到,因为小数点具有其他意义(底下会介绍),所以必须要使用转义字符(\)来加以解除其特殊意义!
找出空白行:
[root@www ~]#grep-n'^$' regular_express.txt22:
因为只有行首跟行尾 (^$) , 所以,这样就可以找出空白行啦!
任意一个字节 . 与重复字节 *
这两个符号在正则表达式的意义如下:
. (小数点):代表『一定有一个任意字节』的意思;* (星号):代表『重复前一个字符,0到无穷多次』的意思,为组合形态
假设我需要找出 g??d 的字串,亦即共有四个字节,起头是 g 而结束是 d,我可以这样做:
[root@www ~]#grep-n'g..d' regular_express.txt1:"Open Source" is a good mechanism to develop programs.9:Oh! The soup taste good.16:The worldis 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.txt1:"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 toolsfor search keyword.19:goooooogle yes!
如果我想要字串开头与结尾都是 g,但是两个 g 之间仅能存在至少一个 o,亦即是 gog, goog, gooog.... 等等,那该如何?
[root@www ~]#grep-n'goo*g' regular_express.txt18:google is the best toolsfor search keyword.19:goooooogle yes!
如果我想要找出 g 开头与 g 结尾的行 , 当中的字符可有可无
[root@www ~]#grep-n'g.*g' regular_express.txt1:"Open Source" is a good mechanism to develop programs.14:The gd software is a libraryfor drafting programs.18:google is the best toolsfor search keyword.19:goooooogle yes!20:go! go! Let's go.
因为是代表 g 开头与 g 结尾,中间任意字节均可接受,所以,第 1, 14, 20 行是可接受的喔! 这个 .* 的 RE 表示任意字符是很常见的.
如果我想要找出『任意数字』的行?因为仅有数字,所以就成为:

推荐阅读