Linux基础正则表达式之grepegrep
Posted 江左风华
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Linux基础正则表达式之grepegrep相关的知识,希望对你有一定的参考价值。
一、基础正则表达式
- 正则表达式是由一类特殊字符及文本字符所编写的模式,其中有些字符不表示字符的字面意义,而表示控制或统配的功能,支持grep、sed、awk、vim;
- 正则表达式是用于描述字符排列和匹配模式的一种语法规则,它主要用于字符串的模式分割、匹配、查找已经替换等操作;
- grep -v 反向搜索
-i 忽略大小写
-n 显示匹配的行号
-c 统计匹配的行数,不显示搜索结果
-o 仅显示匹配到的字符
-q 不显示结果
(1)^ :匹配以什么开头 --- ^ab表示匹配以ab开头的行;
(2)$:匹配以什么结尾的行 --- ab$表示匹配以 ab结尾的行;
(3)^$:匹配空行 --- 表示匹配空行,不匹配空格;
(4). :匹配任意单个字符 --- ab.表示表示匹配abc或abc,不匹配abcd或abce,包括空格
(5) :转义符,将特殊符号进行转义 ------ a.b表示匹配a.b的行,转义点的实际属性,而不代表任意字符;
(6)* :匹配前面项0次或者多次 --- ab* 表示匹配a或者ab或者b或者abb,只要匹配ab字符其中一次或者多次的行;
(7).* :匹配任意字符 --- ab.* 表示匹配ab或者或者abc或者abd,包括空格;
(8)[] :匹配集合以内的任意单个字符 --- ab[cd] 表示匹配abc或者abc,不匹配abe或者abf;
(9)[^] :匹配集合以外的任意单个字符 ---ab[^cd] b表示匹配abe或abf,不匹配abc,abd;
1、表达式测试数据
[[email protected] /]# cat test
I am oldboy teacher !
Iteach linux.
I like badminton ball,billard ball and chinese chess.
my blog is http//:oldboy.blog.51cto.com
our site is http://www.etiantian.org
my qq num is 49000048
not 4900000048
my god,i am not oldbey,but OLDBOY
oldb y
(1)^ :匹配以什么开头
[[email protected] /]# grep "^m" test
my blog is http//:oldboy.blog.51cto.com
my qq num is 49000048
my god,i am not oldbey,but OLDBOY
(2)$:匹配以什么结尾的行
[[email protected] /]# grep "m$" test
my blog is http//:oldboy.blog.51cto.com
(3)^$:匹配空行(用-vn参数排查空行,只显示非空行)
[[email protected] /]# grep -vn "^$" test
1:I am oldboy teacher !
2:Iteach linux.
5:I like badminton ball,billard ball and chinese chess.
7:my blog is http//:oldboy.blog.51cto.com
8:our site is http://www.etiantian.org
12:my qq num is 49000048
15:not 4900000048
16:my god,i am not oldbey,but OLDBOY
18:oldb y
(4). :匹配任意单个字符
[[email protected] /]# grep "m." test
I am oldboy teacher !
I like badminton ball,billard ball and chinese chess.
my blog is http//:oldboy.blog.51cto.com
my qq num is 49000048
my god,i am not oldbey,but OLDBOY
(5) :转义符,将特殊符号进行转义
[[email protected] /]# grep ".$" test #以点结尾的行
Iteach linux.
I like badminton ball,billard ball and chinese chess.
(6)* :匹配前面项0次或者多次
[[email protected] /]# grep -o "0*" test #精确匹配显示匹配的数据
0000
000000
(7).* :匹配任意字符
[[email protected] /]# grep ".*" test 全部匹配,包括空格
I am oldboy teacher !
Iteach linux.
I like badminton ball,billard ball and chinese chess.
my blog is http//:oldboy.blog.51cto.com
our site is http://www.etiantian.org
my qq num is 49000048
not 4900000048
my god,i am not oldbey,but OLDBOY
oldb y
(8)[] :匹配集合以内的任意单个字符
[[email protected] /]# grep "[0-9]" test #匹配有数字的行
my blog is http//:oldboy.blog.51cto.com
my qq num is 49000048
not 4900000048
(9)[^] :匹配集合以外的任意单个字符
[[email protected] /]# grep "[^0-9]" test #匹配非数字的行;
I am oldboy teacher !
Iteach linux.
I like badminton ball,billard ball and chinese chess.
my blog is http//:oldboy.blog.51cto.com
our site is http://www.etiantian.org
my qq num is 49000048
not 4900000048
my god,i am not oldbey,but OLDBOY
oldb y
二、扩展正则表达式
测试数据
[[email protected] ~]# cat test1
I am oldboy teacher !
Iteach linux.
I like badminton ball,billard ball and chinese chess.
my blog is http//:oldboy.blog.51cto.com
our site is http://www.etiantian.org
my qq num is 49000048
not 4900000048
my god,i am not oldbey,but OLDBOY
good
goood
gd
(1)+ :匹配前面1次或多次 ---ab+ 表示匹配ab或abb,不匹配a;
[[email protected] ~]# grep -E "go+d" test1
my god,i am not oldbey,but OLDBOY
good
goood
(2)?:匹配前面项0次或者1次 ---ab?表示匹配a或者ab,不匹配abb;
[[email protected] ~]# grep -E "go?d" test1
my god,i am not oldbey,but OLDBOY
gd
(3)| :匹配两边的任意一项 ---ab|cd 表示匹配ab或者cd;
[[email protected] ~]# grep -E "god|good" test1
my god,i am not oldbey,but OLDBOY
good
(4)() :匹配表达式 ---a(cd)e 表示匹配ace或ade,不匹配ae;
[[email protected] ~]# grep -E "g(a|oo)d" test1
good
[[email protected] ~]# grep -E "g(oo)?d" test1
good
gd
(5){n,m} :匹配前面项n~m次 ---ab{2,3}表示匹配abb或abbb,需要对{}进行转义;
[[email protected] ~]# grep -E "0{3,5}" test1
my qq num is 49000048
not 4900000048
(6){n,} :匹配前面项至少n次,包含n次 ---ab{2,} 表示匹配abb或abbb,需要对{}进行转义;
[[email protected] ~]# grep -E "0{3,}" test1
my qq num is 49000048
not 4900000048
(7){n} :匹配前面项n次 ---ab{2}匹配abb,需要对{}进行转义;
[[email protected] ~]# grep -E "O{1}" test1
my god,i am not oldbey,but OLDBOY
(8){,m}: 匹配前面项最多m次,包含m次, ---ab{,2}匹配a或者ab或者abb,需要对{}转义;
以上是关于Linux基础正则表达式之grepegrep的主要内容,如果未能解决你的问题,请参考以下文章
llinux文本三剑客之grepegrep及相应的正则表达式和用法