grep正则表达式入门实例教程

发布时间:2019-12-24编辑:脚本学堂
分享下grep命令与正则表达式的用法,grep正则表达式元字符集,POSIX字符类,Grep命令选项等内容,感兴趣的朋友参考下。

本节内容:
grep正则表达式入门

1、grep正则表达式元字符集(基本集)
 

^ 锚定行的开始 如:'^grep'匹配所有以grep开头的行。
$ 锚定行的结束 如:'grep$'匹配所有以grep结尾的行。
. 匹配一个非换行符的字符 如:'gr.p'匹配gr后接一个任意字符,然后是p。
* 匹配零个或多个先前字符 如:'*grep'匹配所有一个或多个空格后紧跟grep的行。 .*一起用代表任意字符。
[] 匹配一个指定范围内的字符,如'[Gg]rep'匹配Grep和grep。
[^] 匹配一个不在指定范围内的字符,如:'[^A-FH-Z]rep'匹配不包含A-R和T-Z的一个字母开头,紧跟rep的行。
(..) 标记匹配字符,如'(love)',love被标记为1。
< 锚定单词的开始,如:'<grep'匹配包含以grep开头的单词的行。
> 锚定单词的结束,如'grep>'匹配包含以grep结尾的单词的行。
x{m} 重复字符x,m次,如:'0{5}'匹配包含5个o的行。
x{m,} 重复字符x,至少m次,如:'o{5,}'匹配至少有5个o的行。
x{m,n} 重复字符x,至少m次,不多于n次,如:'o{5,10}'匹配5--10个o的行。
w 匹配文字和数字字符,也就是[A-Za-z0-9],如:'Gw*p'匹配以G后跟零个或多个文字或数字字符,然后是p。
W w的反置形式,匹配一个或多个非单词字符,如点号句号等。
b 单词锁定符,如: 'bgrepb'只匹配grep。

2、用于egrep和 grep -E的元字符扩展集
 

+ 匹配一个或多个先前的字符。如:'[a-z]+able',匹配一个或多个小写字母后跟able的串,如loveable,enable,disable等。
? 匹配零个或多个先前的字符。如:'gr?p'匹配gr后跟一个或没有字符,然后是p的行。
a|b|c 匹配a或b或c。如:grep|sed匹配grep或sed
() 分组符号,如:love(able|rs)ov+匹配loveable或lovers,匹配一个或多个ov。
x{m},x{m,},x{m,n} 作用同x{m},x{m,},x{m,n}

3、POSIX字符类

为了在不同国家的字符编码中保持一至,POSIX(The Portable Operating System Interface)增加了特殊的字符类,如[:alnum:]是A-Za-z0-9的另一个写法。要把它们放到[]号内才能成为正则表达式,如[A- Za-z0-9]或[[:alnum:]]。在linux下的grep除fgrep外,都支持POSIX的字符类。
 

[:alnum:] 文字数字字符
[:alpha:] 文字字符
[:digit:] 数字字符
[:graph:] 非空字符(非空格、控制字符)
[:lower:] 小写字符
[:cntrl:] 控制字符
[:print:] 非空字符(包括空格)
[:punct:] 标点符号
[:space:] 所有空白字符(新行,空格,制表符)
[:upper:] 大写字符
[:xdigit:] 十六进制数字(0-9,a-f,A-F)

4、grep命令选项
 

-? 同时显示匹配行上下的?行,如:grep -2 pattern filename同时显示匹配行的上下2行。
-b,--byte-offset 打印匹配行前面打印该行所在的块号码。
-c,--count 只打印匹配的行数,不显示匹配的内容。
-f File,--file=File 从文件中提取模板。空文件中包含0个模板,所以什么都不匹配。
-h,--no-filename 当搜索多个文件时,不显示匹配文件名前缀。
-i,--ignore-case 忽略大小写差别。
-q,--quiet 取消显示,只返回退出状态。0则表示找到了匹配的行。
-l,--files-with-matches 打印匹配模板的文件清单。
-L,--files-without-match 打印不匹配模板的文件清单。
-n,--line-number 在匹配的行前面打印行号。
-s,--silent 不显示关于不存在或者无法读取文件的错误信息。
-v,--revert-match 反检索,只显示不匹配的行。
-w,--word-regexp 如果被<和>引用,就把表达式做为一个单词搜索。
-V,--version 显示软件版本信息。

5、 实例

要用好grep这个工具,其实就是要写好正则表达式,所以这里不对grep的所有功能进行实例讲解,只列几个例子,讲解一个正则表达式的写法。
$ ls -l | grep '^a'
通过管道过滤ls -l输出的内容,只显示以a开头的行。
$ grep 'test' d*
显示所有以d开头的文件中包含test的行。
$ grep 'test' aa bb cc
显示在aa,bb,cc文件中匹配test的行。
$ grep '[a-z]{5}' aa
显示所有包含每个字符串至少有5个连续小写字符的字符串的行。
$ grep 'w(es)t.*1' aa
如果west被匹配,则es就被存储到内存中,并标记为1,然后搜索任意个字符(.*),这些字符后面紧跟着另外一个es(1),找到就显示该行。如果用egrep或grep -E,就不用""号进行转义,直接写成'w(es)t.*1'就可以了。

grep 工具,以前介绍过。
grep -[acinv] '搜索内容串' filename
 

-a 以文本文件方式搜索
-c 计算找到的符合行的次数
-i 忽略大小写
-n 顺便输出行号
-v 反向选择,即找 没有搜索字符串的行
 

其中搜索串可以是正则表达式!

1,搜索有the的行,并输出行号
$grep -n 'the' regular_express.txt
搜 索没有the的行,并输出行号
$grep -nv 'the' regular_express.txt

2,利 用[]搜索集合字符
[] 表示其中的某一个字符 ,例如[ade] 表示a或d或e
 

root@jbxue:~/tmp$ grep -n 't[ae]st' regular_express.txt
8:I can't finish the test.
9:Oh! the soup taste good!

可以用^符号做[]内的前缀,表示除[]内的字符之外的字 符。
比如搜索oo前没有g的字符串所在的行. 使用 '[^g]oo' 作搜索字符串
 

root@jbxue:~/tmp$ 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!

[] 内可以用范围表示,比如[a-z] 表示小写字母,[0-9] 表示0~9的数字, [A-Z] 则是大写字母们。[a-zA-Z0-9]表示所有数字与英文字符。 当然也可以配合^来排除字符。
搜索包含数字的行
 

root@jbxue:~/tmp$ grep -n '[0-9]' regular_express.txt
5:However ,this dress is about $ 3183 dollars.
15:You are the best is menu you are the no.1.

行首与行尾字符 ^ $. ^ 表示行的开头,$表示行的结尾( 不是字符,是位置)那么‘^$' 就表示空行,因为只有
行首和行尾。
这里^与[]里面使用的^意义不同。它表示^后面的串是在行的开头。
比如搜索the在开头的行
 

root@jbxue:~/tmp$ grep -n '^the' regular_express.txt
12:the symbol '*' is represented as star.

搜索以小写字母开头的行
 

root@jbxue:~/tmp$ 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 star.
18:google is the best tools for search keyword.
19:goooooogle yes!
20:go! go! Let's go.
root@jbxue:~/tmp$

搜索开头不是英文字母的行
 

root@jbxue:~/tmp$grep -n '^[^a-zA-Z]' regular_express.txt
1:"Open Source" is a good mechanism to develop programs.
21:#I am VBird
 

root@jbxue:~/tmp$ $表示它前面的串是在行的结尾,比如 '.' 表示 . 在一行的结尾
搜索末尾是.的行
 

root@jbxue:~/tmp$ 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.
5:However ,this dress is about $ 3183 dollars.
6:GNU is free air not free beer.
.....

注意在MS的系统下生成的文本文件,换行会加上一个 ^M 字符。所以最后的字符会是隐藏的^M ,在处理Windows
下面的文本时要特别注意!
可以用cat dos_file | tr -d 'r' > unix_file 来删除^M符号。 ^M==r

那么'^$' 就表示只有行首行尾的空行拉!
搜索空行
 

root@jbxue:~/tmp$ grep -n '^$' regular_express.txt
22:
23:
 

搜索非空行
 

root@jbxue:~/tmp$ grep -vn '^$' 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.
..........

任意一个字符. 与重复字符 *

在bash中*代表通配符,用来代表任意个 字符,但是在正则表达式中,他含义不同,*表示有0个或多个 某个字符。
例如 oo*, 表示第一个o一定存在,第二个o可以有一个或多个,也可以没有,因此代表至少一个o.

点. 代表一个任意字符,必须存在。 g??d 可以用 'g..d' 表示。 good ,gxxd ,gabd .....都符合。
 

root@jbxue:~/tmp$ 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 is the same with 'glad'.
root@jbxue:~/tmp$

搜索两个o以上的字符串
 

root@jbxue:~/tmp$ grep -n 'ooo*' regular_express.txt //前两个o一定存在,第三个o可没有,也可有多个。
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开头和结尾,中间是至少一个o的字符串,即gog, goog....gooog...等
 

root@jbxue:~/tmp$ grep -n 'goo*g' regular_express.txt
18:google is the best tools for search keyword.
19:goooooogle yes!

搜索g开头和结尾的字符串在的行
 

root@jbxue:~/tmp$ grep -n 'g.*g' regular_express.txt // .*表示 0个或多个任意字符
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.

限定连续重复字符的范围 { }
. * 只能限制0个或多个, 如果要确切的限制字符重复数量,就用{范围} 。范围是数字用,隔开 2,5 表示2~5个,
2表示2个,2, 表示2到更多个
注意,由于{ }在shell中有特殊意义,因此作为正则表达式用的时候要用转义一下。

搜索包含两个o的字符串的行。
 

root@jbxue:~/tmp$ 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 keyword.
19:goooooogle yes!

搜索g后面跟2~5个o,后面再跟一个g的字符串的行。
 

root@jbxue:~/tmp$ grep -n 'go{2,5}g' regular_express.txt
18:google is the best tools for search keyword.

搜索包含g后面跟2个以上o,后面再跟g的行。。
 

root@jbxue:~/tmp$ grep -n 'go{2,}g' regular_express.txt
18:google is the best tools for search keyword.
19:goooooogle yes!

注意,相让[]中的^ - 不表现特殊意义,可以放在[]里面内容的后面。
'[^a-z.!^ -]' 表示没有小写字母,没有. 没有!, 没有空格,没有- 的 串,注意[]里面有个小空格。

另外shell 里面的反向选择为[!range], 正则里面是 [^range]

2、扩展正则表达式

扩展正则表达式是对基础正则表达式添加了几个特殊构成的。
它令某些操作更加方便。
比如我们要去除 空白行和行首为 #的行, 会这样用:
 

root@jbxue:~/tmp$ grep -v '^$' regular_express.txt | grep -v '^#'
"Open Source" is a good mechanism to develop programs.
apple is my favorite food.
Football game is not use feet only.
this dress doesn't fit me.
............

然而,使用支持扩展正则表达式的 egrep 与扩展特殊符号 | ,会方便许多。
注意grep只支持基础表达式, 而egrep 支持扩展的, 其实 egrep 是 grep -E 的别名而已。因此grep -E 支持扩展正则。
那么:
 

root@jbxue:~/tmp$ egrep -v '^$|^#' regular_express.txt
"Open Source" is a good mechanism to develop programs.
apple is my favorite food.
Football game is not use feet only.
this dress doesn't fit me.
....................

这里| 表示或的关系。 即满足 ^$ 或者 ^# 的字符串。

几个扩展特殊符号:
+, 于 . * 作用类似,表示 一个或多个重复字符。
?, 于 . * 作用类似,表示0个或一个字符。
|,表示或关系,比如 'gd|good|dog' 表示有gd,good或dog的串
(),将部分内容合成一个单元组。 比如 要搜索 glad 或 good 可以这样 'g(la|oo)d'
()的好处是可以对小组使用 + ? * 等。
比如要搜索A和C开头结尾,中间有至少一个(xyz) 的串,可以这样 : 'A(xyz)+C'