[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::/://