[toc]正则表达式
正则就是一串有规律的字符串shell
掌握好正则对于编写shell脚本有很大帮助编程
各类编程语言中都有正则,原理是同样的vim
本章将要学习grep/egrep、sed、awkbash
grep命令构成,在CentOS7中自带颜色编程语言
[root@xaviyunserver grep]# which grep alias grep='grep --color=auto' /usr/bin/grep
举例:新建一个grep目录,将etc/passwd目录下的内容拷贝到grep目录下工具
[root@localhost ~]# mkdir grep [root@localhost ~]# cd grep/ [root@localhost grep]# cp /etc/passwd . //建议此处改成cp /etc/passwd test.txt,防止误操做 [root@localhost grep]# ls passwd
说明,gerp默认匹配到的字符串标注为红色,用which命令看一下,是带颜色自动显示的
学习
[root@localhost grep]# grep -c 'nologin' passwd 38
若是不加r则显示:测试
[root@localhost grep]# grep 'root' /etc/ grep: /etc/: 是一个目录
[root@localhost grep]# cp /etc/inittab ./ //拷贝的测试目录grep下
编辑inittab文件,把带#号的两行修改了,方便测试 这在后续查找文件时,方便阅读
.net
[root@localhost grep]# grep -v '^#' 1.txt |grep -v '^$' deda deda Deded D DdFFF1212 121324# 232 wq3 ewdsd
[root@localhost grep]# grep -v '^[0-9]' 1.txt deda deda ### Deded D $21212
[root@localhost grep]# grep -v '[^0-9]' 1.txt 232 121342 [root@localhost grep]# grep '[^0-9]' 1.txt deda deda ### Deded D DdFFF1212 121324# wq3 ewdsd $21212 &32323 DED eweq
[ ] grep 'r.o' passwd //‘r.o’中.表示任意一个字符 为方便测试须要vim其中的passwd文件,并添加r&o等特殊字符
[ ] grep 'oo*' passwd passwd,表示零个或多个前面的字符,0~n个o
[root@localhost grep]# grep 'oo*' passwd|wc -l 46 //结果相同 [root@localhost grep]# grep 'o*o' passwd|wc -l 46
[ ] grep '.*' passwd passwd,.*表示零个或多个任意字符,空行也包含在内,这样就把passwd里面全部的行都匹配到。
[ ] grep 'o{2}' passwd passwd,这里{},其内部为数字,表示前面的字符要重复的次数,须要强调的是{}左右都要加上脱义字符,另外{}还能够表示一个范围,{1,3}表示重复1到3次前面的字符,{1,}表示大于1次重复前面字符
[root@localhost grep]# grep 'o\{2\}' 1.txt dederoooooo rooooooooooooooot:Z:SZXX:XD::X:SX:rooooot
== grep 'o\{2\}' passwd == grep -E 'o{2}' /etc/passwd
[root@localhost grep]# grep 'o\+o' passwd |wc -l 6 [root@localhost grep]# egrep 'o+o' passwd |wc -l 6
[root@localhost grep]# grep 'r.o' test.txt root:x:0:0:root:/root:/bin/bash operator:x:11:0:operator:/root:/sbin/nologin [root@localhost grep]# vim 1.txt [root@localhost grep]# egrep 'o+' 1.txt dederoooooo rooooooooooooooot:Z:SZXX:XD::X:SX:rooooot rot:s3we3e:e3e:sw4e opertor:2323:323:DEX:WSsw:wewe [root@localhost grep]# egrep 'oo+' 1.txt dederoooooo rooooooooooooooot:Z:SZXX:XD::X:SX:rooooot [root@localhost grep]# egrep 'ooo+' 1.txt dederoooooo rooooooooooooooot:Z:SZXX:XD::X:SX:rooooot
== grep -E
[root@localhost grep]# egrep 'root|nologin' passwd|wc -l 39 [root@localhost grep]# grep -E 'root|nologin' passwd|wc -l 39
20.用( )表示一个总体,例如(oo)+就表示1个 ‘oo’ 或者多个 ‘oo’
[root@localhost ~]# mkdir sed [root@localhost ~]# cd sed [root@localhost sed]# cp ../grep/passwd test.txt [root@localhost sed]# sed -n '/root/'p test.txt root:x:0:0:root:/root:/bin/bash operator:x:11:0:operator:/root:/sbin/nologin
[root@localhost sed]# sed -n '/o+t/'p test.txt [root@localhost sed]# sed -nr '/o+t/'p test.txt root:x:0:0:root:/root:/bin/bash operator:x:11:0:operator:/root:/sbin/nologin setroubleshoot:x:991:986::/var/lib/setroubleshoot:/sbin/nologin
#sed -n '2'p test.txt //打印第二行
sed -n '1,$'p test.txt //打印全部行
sed -n '1,5'p test.txt //打印1~5行
[root@localhost sed]# sed -n '1,5'p test.txt root:x:0:0:root:/root:/bin/bash adsda:deda:deded:road:ded sasdda:deda:&&DE:r&o:r<xo: bin:x:1:1:bin:/bin:/sbin/nologin daemon:x:2:2:daemon:/sbin:/sbin/nologin
[root@localhost sed]# sed -e '1'p -e '/root/'p -n test.txt root:x:0:0:root:/root:/bin/bash root:x:0:0:root:/root:/bin/bash operator:x:11:0:operator:/root:/sbin/nologin [root@localhost sed]# sed -e '1'p -e '/bus/'p -n test.txt root:x:0:0:root:/root:/bin/bash systemd-bus-proxy:x:999:997:systemd Bus Proxy:/:/sbin/nologin dbus:x:81:81:System message bus:/:/sbin/nologin
sed -i '1,10'd test.txt //删除1到10行
[root@localhost sed]# wc -l test.txt 48 test.txt [root@localhost sed]# sed -i '1,10'd test.txt [root@localhost sed]# wc -l test.txt 38 test.txt
删除和user(字符串)相关的
[root@localhost sed]# sed -i '/user*/'d test.txt [root@localhost sed]# wc -l test.txt 32 test.txt
#sed ‘1,10s/sbin/sbbin/g’ test.txt 这里使用/做为分隔符,还能够是用其余特殊字符,#和&
#sed -r 's/([^:]+):(.*):([^:]+)/\3:\2:\1/' // s是替换,^:非冒号字符串,.*非特殊符号的字符串,这里会遍历到随后一个冒号前。转义字符,替换成\3,\2,\1的形式
[root@localhost sed]# head test.txt |sed 's/[a-zA-Z]//g' ::0:0::/:// :::: ::&&:&:<: ::1:1::/:// ::2:2::/:// ::3:4:://:// ::4:7::///:// ::5:0::/:// ::6:0::/:// ::7:0::/://