shell正则表达式概述【四】

【1】
###########################正则表达式概述#######################
*******************************基础正则表达式的常见元字符***************************
^、$、.、\、*、[ ]、[^ ]、[n1-n2]、{n}、{n,}、{n,m}
*******************************扩展正则表达式的常见元字符***************************
元字符                作用
+                     重复一个或者一个以上的前一个字符
?                    零个或者一个的前一个字符
|                     使用或者(or)的方式找出多个字符
( )                   查找“组”字符串
( )+                  辨别多个重复的组

*****************************************************************************************
[root@localhost opt]# cd /opt
[root@localhost opt]vi /opt/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 limit.
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.

【2】
########################查找特定字符the#########################
n:--表示显示行号  
i :--表示不区分大小写   
v:--表示反向选择
**********************************n:--表示显示行号  *********************************
[root@localhost opt]# 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 the limit.

**********************************i :--表示不区分大小写 The**************************
[root@localhost opt]# 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 the limit.

***********************************v:--表示反向选择**********************************
反向选择则找不到the
[root@localhost opt]# 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.

【3】
####################利用中括号'[]'来查找集合字符#####################
查找short和shirt两个字符串
用[io]表示匹配'i'或者'o
*****************************[io]表示匹配'i'或者'o'*************************************
[root@localhost opt]# grep -n 'sh[io]rt' test.txt
1:he was short and fat.
2:He was wearing a blue polo shirt with black pants.

*****************************查找重复单个字符'oo'*************************************
[root@localhost opt]#  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'前面不是'w'的字符串****************************
[^]:反向选择
^[ ]:  定位行首
'[^w]oo':前面不是'w'的字符串

[root@localhost opt]#  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!

******************************'[^a-z]查找oo前面不带小写字母的***********************
[root@localhost opt]#  grep -n '[^a-z]oo' test.txt  
3:The home of Football on BBC Sport online.

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

【4】
##########################查找'^' 与行尾字符'$'#####################
'^':行首
'$' :  行尾
查找“the”行首的行
[root@localhost opt]# grep -n '^the' test.txt
4:the tongue is boneless but it breaks bones.12!

***********************************查找行首行以小写字母开头的************************
^[a-z]:查找小写字母开头。

[root@localhost opt]#  grep -n '^[a-z]' test.txt
1:he was short and fat.
4:the tongue is boneless but it breaks bones.12!
5:google is the best tools for search keyword.
8:a wood cross!

***********************************查找行首行不以小写字母开头的**********************
 '^[^a-z]':查找大字母开头.

[root@localhost opt]#   grep -n '^[^a-z]' test.txt
2:He was wearing a blue polo shirt with black pants.
3:The home of Football on BBC Sport online.
6:The year ahead will test our political establishment to the limit.
7:PI=3.141592653589793238462643383249901429
9:Actions speak louder than words
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.

************************************查找首行以大写字母开头****************************
'^[A-Z]':查找大字母开头.

[root@localhost opt]# grep -n '^[A-Z]' test.txt  
2:He was wearing a blue polo shirt with black pants.
3:The home of Football on BBC Sport online.
6:The year ahead will test our political establishment to the limit.
7:PI=3.141592653589793238462643383249901429
9:Actions speak louder than words
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.

**********************************除了以大小写字符开头********************************
 '^[^a-zA-Z]':除了以大小写字符开头

[root@localhost opt]# grep -n '^[^a-zA-Z]' test.txt  
11:#woood #
12:#woooooood #

【5】
#########################转义字符\和末行定位符$###################
查询小数点“.”结尾的行
$:查找结尾行使用定位符“$”
[root@localhost opt]#  grep -n '\.$' 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.
5:google is the best tools for search keyword.
6:The year ahead will test our political establishment to the limit.
15:Misfortunes never come alone/single.
16:I shouldn't have lett so tast.

**********************************^$'查询空白行******************************************
^$':查询空白行
[root@localhost opt]# grep -n '^$' test.txt  
10:

************************************一个字符"."和重复字“*”***************************
查找一个字符"."和重复字“*”            OO*则表示联系多个o的数据捞出来
o*:零个(空字符) oo*:第一个必须存在“o”

[root@localhost opt]# 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!

********************************'w..d'四个字符******************************************
'w..d':w开头d结尾

[root@localhost opt]# 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

*************************************中间至少一个"o"**********************************
[root@localhost opt]#  grep -n 'woo*d' test.txt
8:a wood cross!
11:#woood #
12:#woooooood #

************************************中间可有可无的字符串******************************
[root@localhost opt]# 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 #

************************************查询任意数字所在行********************************
[root@localhost opt]# grep -n '[0-9][0-9]*' test.txt
4:the tongue is boneless but it breaks bones.12!
7:PI=3.141592653589793238462643383249901429

【6】
#########################查找连续字符范围'{}'######################
查询连续2个o字符

[root@localhost opt]# 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!

***********************************查询两个连续“t”字符******************************
[root@localhost opt]#  grep -n 't\{2\}' test.txt   
16:I shouldn't have lett so tast.

**********************************中间包含2到5个o*************************************
 [root@localhost opt]# grep -n 'wo\{2,5\}d' test.txt
8:a wood cross!
11:#woood #

*********************************中间2以上的o字符************************************
[root@localhost opt]# grep -n 'wo\{2,\}d' test.txt
8:a wood cross!
11:#woood #
12:#woooooood #

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值