正则表达式

部分基本规则如下例程所示:

#!/bin/bash     

echo "This is a test!" | sed -n '/test/p'                #This is a test!  
echo "This is a tests!" | gawk '/test/{print $0}'       #This is a tests! 部分匹配     
echo "This is a  test!" | sed -n '/  /p'                 #This is a  test! 空格==字符
#以下特殊字符需要加\转义
echo "This is * "   | sed -n '/\*/p'                     #This is * 
echo "This is [] "  | sed -n '/\[/p'                     #This is [] 
echo "This is ^ "   | sed -n '/\^/p'                     #This is ^
echo "This is $ "   | sed -n '/\$/p'					 #This is $
echo "This is {} "  | sed -n '/{/p'                      #This is {} 
echo "This is \ "   | sed -n '/\\/p'                     #This is \ 
echo "This is / "   | sed -n '/\//p'                     #This is /
echo "This is + "   | sed -n '/\+/p'                     #This is + 
echo "This is ? "   | sed -n '/\?/p'                     #This is ?
echo "This is | "   | sed -n '/\|/p'                     #This is |
echo "This is () "  | sed -n '/)/p'                      #This is () 

echo "this is a test@" | sed -n '/^this/p'               #this is a test@ 头部匹配
echo "haha , this is a test@" | sed -n '/^this/p'        #不匹配

echo "@this is a test" | sed -n '/test$/p'              #@this is a test 尾部匹配      
echo "@this is a tests" | sed -n '/test$/p'                 #不匹配   

#sed '/^$/d' file                                        #删除file文件中的空行

echo "this is a cat" | sed -n '/.at/p'                   #this is a cat     必须匹配一个字符
echo "this is a at" | sed -n '/.at/p'                    #this is a at      该字符可为空格

echo "heow" | sed -n '/hel*ow/p'                         #heow            0次/多次            
echo "hellllow" | sed -n '/hel*ow/p'                     #hellllow        0次/多次  

echo "HEow" | gawk '/HEl?ow/{print $0}'                  #HEow            0次/1次
echo "HElow" | gawk '/HEl?ow/{print $0}'                 #HElow           0次/1次
echo "HEllow" | gawk '/HEl?ow/{print $0}'                #不匹配           

echo "fuccck" | gawk '/uc+k/{print $0}'                  #fuccck          1次/多次 
echo "fuak" | gawk '/u[ac]+k/{print $0}'                 #fuak            1次/多次  字符组匹配 
echo "fuk" | gawk '/u[ac]+k/{print $0}'                     #不匹配         1次/多次             

echo "cao" | gawk --re-interval '/ca{1}o/{print $0}'     #cao             仅出现1次  
echo "caaaao" | gawk --re-interval '/ca{1,3}o/{print $0}' #不匹配         出现1~3次,此处4次

echo "Yes" | sed -n '/[Yy][Ee]s/p'                       #Yes             字符组匹配,可接受大小写格式    
echo "this is 3p,hoho!" | sed -n '/[0123]p/p'            #this is 3p,hoho!

echo "213300" | sed -n '/[0-9][0-9][0-9][0-9][0-9]/p'    #213300           区间匹配   
echo "313300" | sed -n '/^[0-9][0-9][0-9][0-9][0-9]$/p'  #数字个数不匹配   只能5个数字

echo "p1331" | sed -n '/[^0-9][0-9][0-9][0-9][0-9]/p'    #p1331            排除字符组,首字符不能为数字
echo "p1332" | sed -n '/[0-9][0-9][0-9][0-9][0-9]/p'     #不匹配           首位不为数字     
echo "p1333" | sed -n '/[a-z][0-9][0-9][0-9][0-9]/p'     #p1333            首位为字母


echo "Hi,4 !" | sed -n '/[[:alnum:]]/p'                  #Hi,4 !        任意字母/数字:0-9 a-z A-Z 

echo "Hi,1 !" | sed -n '/[[:alpha:]]/p'                  #Hi,1 !        任意字母:     a-z A-Z 
echo "Hi,2 !" | sed -n '/[[:lower:]]/p'                  #Hi,2 !        任意小写字母: a-z  
echo "Hi,3 !" | sed -n '/[[:upper:]]/p'                  #Hi,3 !        任意大写字母: A-Z 

echo "Hi,6 !" | sed -n '/[[:digit:]]/p'                  #Hi,6 !        任意数字: 0-9   

echo "Hi,5 !" | sed -n '/[[:blank:]]/p'                  #Hi,5 !        空格
echo "Hi,7 !" | sed -n '/[[:space:]]/p'                  #Hi,7 !        空格  
echo "Hi,8 !" | sed -n '/[[:punct:]]/p'                  #Hi,8 !        标点符号
echo "Hi,9 !" | sed -n '/[[:print:]]/p'                  #Hi,9 !        可打印字符

echo "The cat is asleep" | gawk '/cat|dog/{print $0}'    #The cat is asleep 管道匹配
echo "The cog is asleep" | gawk '/cat|[dc]og/{print $0}' #The cog is asleep 管道+字符数组匹配

echo "Sat" | gawk '/Sat(urday)?/{print $0}'              #Sat               聚合,接收完整名或者缩写名
echo "Saturday" | gawk '/Sat(urday)?/{print $0}'        #Saturday
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值