1.04101802001-04101802043正则表达式匹配
[root@localhost ~]# echo "04101802001" | grep -oP "^041018020(0[1-9]|[1-3][0-9]|4[0-3])"
04101802001
[root@localhost ~]# echo "04101802044" | grep -oP "^041018020(0[1-9]|[1-3][0-9]|4[0-3])"
[root@localhost ~]# echo "04101802022" | grep -oP "^041018020(0[1-9]|[1-3][0-9]|4[0-3])"
04101802022
2.QQ邮箱
[root@localhost ~]# echo "1422653301@qq.com" | egrep "(^[1-9]*[1-9][0-9]*)@qq.com$"
1422653301@qq.com
3.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