Shell正则表达式(grep)

正则表达式概述

一、正则表达式定义

  • 正则表达式,又称正规表达式、常规表达式
    在代码中经常简写为regex、regexp或RE
  • 是使用单个字符串来描述、匹配一系列符合某个句法或语法规则的字符串
    例: 当邮件服务器过滤垃圾邮件时,会经常使用正则表达式

二、正则表达式组成

(1)普通字符

  • 大小写字母、数字、标点符号及一些其他符号

(2)元字符

  • 在正则表达式中具有特殊意义的专用字符

三、基础正则表达式——grep、sed命令支持

(1)基础正则表达式示例

  • 查找特定字符
  1. -n 显示行号
  2. -i 不区分大小写
  3. -v 反向查找

——创建测试文件 (直接把test.txt的内容复制就行了)

[root@localhost ~]# cat test.txt 
he was short and fat.
He was wearing a blue polo shirt with black pants.
The home of Football on BBC Sport online.
the tongue is boneless but it breaks bones.12!
google is the best tools for search keyword.
The year ahead will test our political establishment to the li
PI=3.141592653589793238462643383249901429
a wood cross!
Actions speak louder than words
#woood #
#woooooood #
AxyzxyzxyzxyzC
I bet this place is really spooky late at night!
Misfortunes never come alone/single.
I shouldn't have lett so tast.

——查找the并显示行号 (之后的操作都是使用test.txt作为模板,可以更加直观一点)

[root@localhost ~]# grep -n 'the' test.txt 
4:the tongue is boneless but it breaks bones.12!
5:google is the best tools for search keyword.
6:The year ahead will test our political establishment to th limit.
`直接'the'筛选the进行过滤`

——查找the并不区分大小写 (每次做的时候可以对照一下模板test.txt)

[root@localhost ~]# grep -in 'the' test.txt 
3:The home of Football on BBC Sport online.
4:the tongue is boneless but it breaks bones.12!
5:google is the best tools for search keyword.
6:The year ahead will test our political establishment to th limit.
`加了-i ,所以不区分大小写`

——反向查找不包含the的行

[root@localhost ~]# grep -vn 'the' test.txt 
1:he was short and fat.
2:He was wearing a blue polo shirt with black pants.
3:The home of Football on BBC Sport online.
7:PI=3.141592653589793238462643383249901429
8:a wood cross!
9:Actions speak louder than words
10:
11:#woood #
12:#woooooood #
13:AxyzxyzxyzxyzC
14:I bet this place is really spooky late at night!
15:Misfortunes never come alone/single.
16:I shouldn't have lett so tast.
`加了-v ,所以变成了反向查找`

(2)利用中括号" [ ] "来查找集合字符

[ ] —— 里面无论有几个字符,都仅代表为一个字符, 相当于“或” 的关系
[^] ——括号里面的 ^ 是取反的意思
——查找包含shirt 或 short 的行 (还是利用刚才的测试文件test.txt进行操作)

[root@localhost ~]# grep -n 'sh[io]rt' test.txt 
1:he was short and fat.
2:He was wearing a blue polo shirt with black pants.
`[io],中括号写的是i和o,所以是i和o都可以,都符合条件`

——查找重复单个字符”oo“的行

[root@localhost ~]# grep -n 'oo' test.txt 
3:The home of Football on BBC Sport online.
5:google is the best tools for search keyword.
8:a wood cross!
11:#woood #
12:#woooooood #
14:I bet this place is really spooky late at night!
`'oo',这里两个o其实和上面的the是同理的`

——查找”oo“前不是”w“的行

[root@localhost ~]# grep -n '[^w]oo' test.txt 
3:The home of Football on BBC Sport online.
5:google is the best tools for search keyword.
11:#woood #
12:#woooooood #
14:I bet this place is really spooky late at night!
`11,12行‘oo’前面不是‘w’,而是‘o’或多个‘o’,所以会显示出来`

——查找”oo“前不是小写字母的行

[root@localhost ~]# grep -n '[^a-z]oo' test.txt 
3:The home of Football on BBC Sport online.
`[^a-z],以a-z开头的小写字母`

——查找”oo“前不是大写字母的行

[root@localhost ~]# grep -n '[^A-Z]oo' test.txt 
5:google is the best tools for search keyword.
8:a wood cross!
11:#woood #
12:#woooooood #
14:I bet this place is really spooky late at night!
`[^A-Z],同理以A-Z开头的大写字母`

——查找包含数字的行

[root@localhost ~]# grep -n '[0-9]' test.txt 
4:the tongue is boneless but it breaks bones.12!
7:PI=3.141592653589793238462643383249901429
`[0-9],包含数字的行`

**(可以按自己的理解试试其他的筛选条件)**

(3)查找行首 ^ 与行尾字符 $

  • 小数点” . “ 在正则表达式中为元字符,需要使用转义字符” \ "将其转化为普通字符
    ——查找以小数点“ . ” 结尾的行
[root@localhost ~]# grep -n '\.$' test.txt 
`查找空行`
[root@localhost ~]# grep -n '^$' test.txt 

(4)查找任意一个字符 ". “与重复字符” * "

  • 查找以“w"开头,”d"结尾共4个字符的行
[root@localhost ~]# grep -n 'w..d' test.txt 
5:google is the best tools for search keyword.
8:a wood cross!
9:Actions speak louder than words
`w..d就是中间的两个'.'表示任意字符`
  • " * "表示重复零个或多个前面的单字符,

——查询至少包含两个o以上的字符串

[root@localhost ~]# grep -n 'ooo*' test.txt 
3:The home of Football on BBC Sport online.
5:google is the best tools for search keyword.
8:a wood cross!
11:#woood #
12:#woooooood #
14:I bet this place is really spooky late at night!
`‘ooo*’---第1个'o'和第二个'o'必须存在,第三个'o'可以是0个或多个,所以oo,ooo等都符合规则`
`同理oo*的话就是第一个'o'必须存在,第二个'o'可以是0个或多个`

——查找以“ w ”开头,中间至少包含一个“ o ”的,以” d “结尾的行

[root@localhost ~]# grep -n 'woo*d' test.txt 
8:a wood cross!
11:#woood #
12:#woooooood #
`这个就是利用了oo*,第一个'o'必须有,如果是中间至少包含两个'o'的话就是wooo*d`

——查找以‘w’开头,‘d’结尾 中间字符可有可无 的行

[root@localhost ~]# grep -n 'w.*d' test.txt 
1:he was short and fat.
5:google is the best tools for search keyword.
8:a wood cross!
9:Actions speak louder than words
11:#woood #
12:#woooooood #
`w.*d 这个'.'后面跟了* ,所以这个'.'可以是0个也可以是多个,说白了就是以'w'开头'd'结尾的不管多少字符都符合`

——查询任意数字的行

[root@localhost ~]# grep -n '[0-9][0-9]*' test.txt 
4:the tongue is boneless but it breaks bones.12!
7:PI=3.141592653589793238462643383249901429
`[0-9][0-9]*第一个[0-9]必须有,第二个可以是0个也可以是多个,所以可以看成只要有数字不管多少字符的行都符合`

(5)查找连续字符范围 { }

**使用 " . "和 " * "可以设置零个或无限多个重复的字符,如果要限制一个范围则使用' { } **
——查看2个o的字符

[root@localhost ~]# grep -n 'o\{2\}' test.txt 
3:The home of Football on BBC Sport online.
5:google is the best tools for search keyword.
8:a wood cross!
11:#woood #
12:#woooooood #
14:I bet this place is really spooky late at night!
`o\{2\}即查找o并且查找字符范围是2两个及以上`

——查看w开头,d结尾,中间为2-5个o的字符串

[root@localhost ~]# grep -n 'wo\{2,5\}d' test.txt 
8:a wood cross!
11:#woood #
`wo\{2,5\}d,2,5及2-5个字符,开头是w结尾是d的`

——查看w开头,d结尾,中间为2以上o的字符串

[root@localhost ~]# grep -n 'wo\{2,\}d' test.txt 
8:a wood cross!
11:#woood #
12:#woooooood #
`wo\{2,\}d,这里2后面跟了','号,就是说2个以上,不加就是两个,参考上面的案例`

四、基础正则表达式的常见元字符

^word       搜索以word开头的。^ 一行的开头。
word$       搜索以word结尾的。$ 一行的结尾。
^$          表示空行
.           代表且只能代表任意一个单个字符
\           例:\. 只代表点本身,转义符号,让有特殊身份意义的字符,脱掉马甲,还原,\.$即查找以.结尾的
\n          换行符
\r          匹配回车
\w          匹配任意一个字符和数字
*           重复0次或多次前面的一个字符。a*则匹配0次或者多次a
.*          匹配所有字符。例:^.* 以任意多个字符开头,.*$以任意多个字符结尾。
[abc]       匹配字符集内的任意一个字符。w[abc]d,即只要是以w开头d结尾的中间存在a、b、c中的任意一个字符都符合
[^abc]      不要abc这三个字符排列组合成的任意一个字符串。中括号里的 ^ 为取反。
[1-9]       只要包含1-9之内的数字,就打印该行
a\{n,m\}    重复n到m次a的行。若用egrep、sed -r可以去掉斜线。
a\{n,\}     重复至少n 次a的行。若用egrep、sed -r可以去掉斜线。
a\{n\}      只打印前边出现的n次a的行。若用egrep、sed -r可以去掉斜线
a\{,n\}     打印前边的那个最多出现n次a的行

五、扩展正则表达式的常见元字符——egrep、awk命令支持

grep使用扩展正则表达式需要加选项-E
+		重复一个或者一个以上的前一个字符
		零个或者一个的前一个字符
|		使用或者(or)的方式找出多个字符
()		查找“组”字符串,这个组视为一个整体,如(ab)那么就只匹配ab,不会匹配ba
()+		辨别多个重复的组
  • 11
    点赞
  • 61
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值