grep及常用正则表达式

前端之家收集整理的这篇文章主要介绍了grep及常用正则表达式前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

一、grep 命令

grep �help查看grep命令发现可选参数太多。这里挑几个常用的来举例说明。

示例文件如下:

[root@localhosttest]#moregrep.txt
xy
yxay
xxxy
yaxy
yyxxyyxx
yyaaxxyyaa
xaaay
axay
xaya
rootxyxyxyxyxyxyx
xyxyxyxyxyxyx
xyxyxyxyxyx
xyxXYXYXYXyxyx
xyxyxyxyxyxyxxy
xyxyxixixixinsnsnsnxixiyxyx
XYXYXxy
XaaaaY
XYXYXYXYXYX
XsdfsfasfY
XXYYYAAAA
XXXAAAAYYY

-c 显示匹配的次数

[root@localhost test]# grep -c ‘xy’grep.txt

12

-i :忽略大小差异

[root@localhost test]# grep -ci ‘xy’grep.txt

14 #发现忽略大小写多了2行。

-n 显示行号

[root@localhosttest]#grep-n'xyxy'grep.txt
10:rootxyxyxyxyxyxyx
11:xyxyxyxyxyxyx
12:xyxyxyxyxyx
14:xyxyxyxyxyxyxxy
15:xyxyxixixixinsnsnsnxixiyxyx

-A+nn代表数字) After的意思,显示匹配字符后n行数据

-B+nn代表数字) Before的意思,显示匹配字符前n行数据

[root@localhosttest]#grep-nA2'xixi'grep.txt
15:xyxyxixixixinsnsnsnxixiyxyx
16-XYXYXxy
17-XaaaaY
[root@localhosttest]#grep-nB2'xixi'grep.txt
13-xyxXYXYXYXyxyx
14-xyxyxyxyxyxyxxy
15:xyxyxixixixinsnsnsnxixiyxyx

-v 显示没有匹配的行。

[root@localhosttest]#grep-nv'x'grep.txt
17:XaaaaY
18:XYXYXYXYXYX
19:XsdfsfasfY
20:XXYYYAAAA
21:XXXAAAAYYY

注意:一般为了显示明显,一般会使用―color=auto。一般系统会通过别名的方式自带此参数,如果没有可以自己添加个别名就可以了:alias grep='grep --color=auto'。还有对于要查找的字符应用‘’(单引号)引起来,一般不建议用双引号,容易被程序误解。

二、基本正则表达式

申明:C:表示单个字符(char),S:表示字符串(String

^S :表示搜索S开头的行。

[root@localhosttest]#grep-n'^xy'grep.txt#查找以xy开头的行
1:xy
11:xyxyxyxyxyxyx
12:xyxyxyxyxyx
13:xyxXYXYXYXyxyx
14:xyxyxyxyxyxyxxy
15:xyxyxixixixinsnsnsnxixiyxyx
[root@localhosttest]#grep-n'^xyx'grep.txt#查找以xyx开头的行
11:xyxyxyxyxyxyx
12:xyxyxyxyxyx
13:xyxXYXYXYXyxyx
14:xyxyxyxyxyxyxxy
15:xyxyxixixixinsnsnsnxixiyxyx

[root@localhost test]# grep -n 'xy^'grep.txt #注意^是放在最前面的。

S$ :搜索S结束的行,与^对应

[root@localhosttest]#grep-n'xyx$'grep.txt#查找以xyx结束的行
10:rootxyxyxyxyxyxyx
11:xyxyxyxyxyxyx
12:xyxyxyxyxyx
13:xyxXYXYXYXyxyx
15:xyxyxixixixinsnsnsnxixiyxyx

[root@localhost test]# grep -n '$xy'grep.txt #同样$需要放在后面,放在字符串前面是不对的

. :点号,匹配任意一个字符,注意只是一个字符

[root@localhosttest]#grep-n'a..x'grep.txt#查找行中有a..x的
6:yyaaxxyyaa

\ :斜杠,转义字符

[root@localhosttest]#grep-n"\""grep.txt#通过转义
25:"xy"
[root@localhosttest]#grep-n'"'grep.txt#可以看到通过单引号引用时不需要转义字符
25:"xy"

[] :匹配[]内的字符中任意一个

[root@localhosttest]#grep-n[higk]grep.txt
15:xyxyxixixixinsnsnsnxixiyxyx
22:x,y,z,e,f,g,h
[c1-c2]:匹配字符范围中的一个字符
[root@localhosttest]#grep-ni[h-k]grep.txt
15:xyxyxixixixinsnsnsnxixiyxyx
22:x,h

[^S] :匹配字符串内字符以外的字符

[root@localhosttest]#grep-ni[^aefjkxyz\'\"]grep.txt
10:rootxyxyxyxyxyxyx
15:xyxyxixixixinsnsnsnxixiyxyx
19:XsdfsfasfY
22:x,h

c\{n1,n2\} :前面的字符重复n1n2次。注意是重复出现

[root@localhosttest]#grep-ni'xa\{3,5\}'grep.txt#重复a3-5次,这里-i忽略大小
7:xaaay
17:XaaaaY
21:XXXAAAAYYY
[root@localhosttest]#grep-ni'xy\{3,5\}'grep.txt#同样重复y3-5次
20:XXYYYAAAA

\<S :匹配文中单词是以S开头的

[root@localhosttest]#grep-n'\<yx'grep.txt
2:yxay
S\>:匹配文中单词是以S结尾的
[root@localhosttest]#grep-n'xyxyx\>'grep.txt
10:rootxyxyxyxyxyxyx
11:xyxyxyxyxyxyx
12:xyxyxyxyxyx

C* :匹配前面的单个字符重复0到多次。即包不包含C字符无所谓

[root@localhosttest]#grep-n'axy*'grep.txt#是否包含y无所谓
4:yaxy
6:yyaaxxyyaa
8:axay

三、扩展正则表达式

egrep :grep命令的扩展命令。与grep�E作用一样。

C+ :重复前面单个字符1到多次。即需包含前面这个字符

[root@localhosttest]#egrep-n'ay+'grep.txt#带有a开始,并且紧跟后面包含至少一个y
2:yxay
7:xaaay
8:axay
9:xaya

C? :匹配0次或1次前面的字符C

[root@localhosttest]#egrep-n'ax?'grep.txt#以带有a开头后跟不跟x无所谓
2:yxay
4:yaxy
6:yyaaxxyyaa
7:xaaay
8:axay
9:xaya
17:XaaaaY
19:XsdfsfasfY
[root@localhosttest]#egrep-n'ax+'grep.txt#带有a开头后紧跟x或者多个x如6
4:yaxy
6:yyaaxxyyaa
8:axay

S|S :匹配前面的字符串或后面字符串

[root@localhosttest]#egrep-n'aaxx|aaaa'grep.txt
6:yyaaxxyyaa
17:XaaaaY

(S) :匹配括号字符串S

[root@localhosttest]#egrep-n'(axx)|(xixi)'grep.txt
6:yyaaxxyyaa
15:xyxyxixixixinsnsnsnxixiyxyx

猜你在找的正则表达式相关文章