shell正则表达式之grep、sed部分示例

1. 04101802001-04101802043正则表达式匹配

[root@servera ~]# echo "04101802001" | egrep "^041018020(0[1-9]|[1-3][0-9]|4[0-3])$"
04101802001
[root@servera ~]# echo "04101802043" | egrep "^041018020(0[1-9]|[1-3][0-9]|4[0-3])$"
04101802043
[root@servera ~]# echo "04101802049" | egrep "^041018020(0[1-9]|[1-3][0-9]|4[0-3])$"
[root@servera ~]# 

解析:
egreg:是一个搜索文件获得模式,使用该命令可以任意搜索文件中的字符串和符号,也可以为你搜索一个多个文件的字符串,一个提示符可以是单个字符、一个字符串、一个字、一个句子。
egrep “^041018020(0[1-9]|[1-3][0-9]|4[0-3])$”
这题当中需要匹配的是最后两位数字,即0[1-9]匹配01-09这个范围;[1-3][0-9]即匹配10-39这个范围;4[0-3]即范围边界40-43

2. qq邮箱地址

qq邮箱地址:2540957086@qq.com

[root@servera ~]# echo "2540957586@qq.com" | egrep "(^[1-9]*[1-9][0-9]*)@qq.com$"
2540957586@qq.com

sed命令使用
地址定界和编辑命令的使用:
#: d命令
i \text命令
c \text命令
#,#: p命令
#~#: s/pattern/string
#,+N: s/pattern/string/g
#,~N: d命令
$:d命令
/regexp/ : a \text
#,/regexp/ : s/pattern/string/g
/regexp/, /regexp/: s/pattern/string/g

[root@servera ~]# cat hello.txt 
welcome to xian
welcome to shenzheng
welcome to chonqing
welcome to chengdu
welcome to xizhang
welcome to wuhan
welcome to guangzhou
welcome to guangxi

验证:
1、d删除匹配的某一行
[root@servera ~]# sed '/wuhan/d' hello.txt 
welcome to xian
welcome to shenzheng
welcome to chonqing
welcome to chengdu
welcome to xizhang
welcome to guangzhou
welcome to guangxi
2、i在匹配行之前追加内容

3、p命令打印当前模式空间内容     
[root@servera ~]# sed -n '1,$p' hello.txt 
welcome to xian
welcome to shenzheng
welcome to chonqing
welcome to chengdu
welcome to xizhang
welcome to wuhan
welcome to guangzhou
welcome to guangxi
注:-n -n参数后,则只有经过sed特殊处理的那一行才会被
列出来;
'1,$p' 表示第一行开始到最后一行打印出来

4、s/pattern/string/g 替换内容
[root@servera ~]# sed 's/wuhan/WUHAN/g' hello.txt 
welcome to xian
welcome to shenzheng
welcome to chonqing
welcome to chengdu
welcome to xizhang
welcome to WUHAN
welcome to guangzhou
welcome to guangxi

5、指定文件中哪一行进行替换,利用#(数字)
[root@servera ~]# sed ' 2 s/welcome/Hi/' hello.txt 
welcome to xian
Hi to shenzheng
welcome to chonqing
welcome to chengdu
welcome to xizhang
welcome to wuhan
welcome to guangzhou
welcome to guangxi

6,删除hello.txt中第4行到末尾行中所有的to
[root@servera ~]# sed '4,$s/to//g' hello.txt 
welcome to xian
welcome to shenzheng
welcome to chonqing
welcome  chengdu
welcome  xizhang
welcome  wuhan
welcome  guangzhou
welcome  guangxi

7、删除hello.txt中2到4行中所有的welcome to;
[root@servera ~]# sed '2,4s/welcome to//g' hello.txt 
welcome to xian
 shenzheng
 chonqing
 chengdu
welcome to xizhang
welcome to wuhan
welcome to guangzhou
welcome to guangxi

8、删除每一行的第一个单词
[root@servera ~]# sed 's/^\<[a-zA-Z0-9]*\>//' hello.txt 
 to xian
 to shenzheng
 to chonqing
 to chengdu
 to xizhang
 to wuhan
 to guangzhou
 to guangxi

9、打印第一行
[root@servera ~]# sed -e '1 p' hello.txt -n
welcome to xian

10、打印行号
[root@servera ~]# sed '=' hello.txt 
1
welcome to xian
2
welcome to shenzheng
3
welcome to chonqing
4
welcome to chengdu
5
welcome to xizhang
6
welcome to wuhan
7
welcome to guangzhou
8
welcome to guangxi

只打印1和5行
[root@servera ~]# sed '1p;3p' hello.txt -n
welcome to xian
welcome to chonqing
方法二
[root@servera ~]# sed '1,8{
> 1p
> 4p
> 6p
> }' hello.txt -n
welcome to xian
welcome to chengdu
welcome to wuhan

把多行转为一行,换行用‘,’替换
[root@servera ~]# sed ':a;N;s/\n/,/;b a' hello.txt 
welcome to xian,welcome to sxhenzheng,welcome to chonqing,welcome to chengdu,welcome to xizhang,welcome to wuhan,welcome to guangzhou,welcome to guangxi
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值