正則表達式之grep,sed

[toc]正則表達式

正則表達式之grep,sed

一 grep

1.1 什麼是正則

正則就是一串有規律的字符串shell

掌握好正則對於編寫shell腳本有很大幫助編程

各類編程語言中都有正則,原理是同樣的vim

本章將要學習grep/egrep、sed、awkbash

1.2 grep介紹

grep命令構成,在CentOS7中自帶顏色編程語言

[root@xaviyunserver grep]# which grep
alias grep='grep --color=auto'
	/usr/bin/grep
  • [ ] grep [-cinvABC] 'word' filename
  • [ ] -c 行數
  • [ ] -i 不區分大小寫
  • [ ] -n 顯示行號
  • [ ] -v 取反,相反的顯示
  • [ ] -r 遍歷全部子目錄及孫目錄
  • [ ] -A 後面跟數字,過濾出符合要求的行以及下面n行
  • [ ] -B 同上,過濾出符合要求的行以及上面n行
  • [ ] -C 同上,同時過濾出符合要求的行以及上下各n行

舉例:新建一個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命令看一下,是帶顏色自動顯示的 mark mark學習

1. grep -c 表示打印符合要求的行數

[root@localhost grep]# grep -c 'nologin' passwd
38

2. grep -n 表示輸出符合要求的行及行號

mark

3. grep -v 表示打印不符合要求的行

mark

4. grep -r 遍歷全部子目錄

mark 若是不加r則顯示:測試

[root@localhost grep]# grep 'root' /etc/
grep: /etc/: 是一個目錄

5. 經過grep遍歷全部root文件並找到與passwd相關的

mark

6.grep -A2 會把包含root的行以及這行下面的兩行打印出來

mark

7.grep -B2 會把包含root的行以及這行上面的兩行打印出來

mark

8. grep -C2 會把包含root的行以及這行上下各兩行打印出來

mark

grep/egrep示例

9. 過濾出帶有某個關鍵詞的行,並輸出行號

  • [ ] grep -n 'root' /etc/passwd

10. 過濾出不帶某個關鍵詞的行,並輸出行號

  • [ ] grep -nv 'nologin' /etc/passwd

11.過濾出全部包含數字的行

  • [ ] grep '[0-9]' passwd

mark

12.過濾全部不包含數字的行

  • [ ] grep -v '[0-9]'/etc/inittab
  • grep -vn 舉例,把不含數字的行打印出來,vim編輯時輸入:set nu 提示行號

mark mark mark

13.過濾掉全部以#開頭的行

  • [ ] grep -v '^#' /etc/inittab 爲了便於測試,須要將/etc/inittab拷貝出來到gerp目錄下
[root@localhost grep]# cp /etc/inittab ./  //拷貝的測試目錄grep下

編輯inittab文件,把帶#號的兩行修改了,方便測試 mark 這在後續查找文件時,方便閱讀 mark.net

13.過濾掉全部的空行和以#開頭的行

  • [ ] grep -v '^#' inittab |grep -v '^$' 在正則表達式中, 「^」 表示行的開始, 「$」 表示行的結尾,那麼空行則能夠用 「^$」 表示。 mark
[root@localhost grep]# grep -v '^#' 1.txt |grep -v '^$'
deda
deda
Deded
D
DdFFF1212
121324#
232
wq3
ewdsd

14. 過濾掉全部不包含數字的行

  • [ ] grep '[^0-9]' inittab mark

15. 過濾全部開頭是0-9的行

  • [ ] grep '^[0-9]' inittab
[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

16. 過濾出任意一個字符和重複字符

  • [ ] grep 'r.o' passwd //‘r.o’中.表示任意一個字符 爲方便測試須要vim其中的passwd文件,並添加r&o等特殊字符 mark

  • [ ] grep 'oo*' passwd passwd,表示零個或多個前面的字符,0~n個o

mark

[root@localhost grep]# grep 'oo*' passwd|wc -l
46 //結果相同
[root@localhost grep]# grep 'o*o' passwd|wc -l
46
  • [ ] grep '.*' passwd passwd,.*表示零個或多個任意字符,空行也包含在內,這樣就把passwd裏面全部的行都匹配到。 mark

  • [ ] 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

mark

mark

  • [ ] egrep 'o{2}' /etc/passwd 等同於上面的命令
== grep 'o\{2\}' passwd == grep -E 'o{2}' /etc/passwd

mark

mark

  • [ ] egrep 'o+o'passwd,+表示匹配1個或者多個+前面的字符
[root@localhost grep]# grep 'o\+o' passwd |wc -l
6
[root@localhost grep]# egrep 'o+o' passwd |wc -l
6

mark

19.egrep過濾出一個或者多個制定的字符

  • [ ] egrep 'o?t' passwd,過濾出零個或者一個指定的字符
[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

mark

  • [ ] egrep 'root|nologin' passwd,|表示或者,能夠有多個|

mark

== 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’

  • [ ] egrep '(oo){2}' passwd mark

sed工具使用

  • sed -n,選項表示只顯示咱們要打印的行,請看-n和沒有-n的區別
[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

mark

  • sed -r 這裏-r和grep裏面-E同樣,還有加上了纔能有效 支持+,*,.,|或者,與等
[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

mark

  • sed打印某行

#sed -n '2'p test.txt //打印第二行

sed -n '1,$'p test.txt //打印全部行

sed -n '1,5'p test.txt //打印1~5行

mark

[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
  • sed -e 能夠實現多個行爲
[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 刪除某些行

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
  • 替換字符或字符串,參數s表示替換的動做,g表示本行全局替換,若是不加g則只替換本行出現的一個,用法同vim

#sed ‘1,10s/sbin/sbbin/g’ test.txt 這裏使用/做爲分隔符,還能夠是用其餘特殊字符,#和& mark

  • 替換擴展 #sed -r '1,10s/ro+/r/g' test.txt |head//要想+這類特殊符號生效必須加上-r,這裏都是臨時打印出來的結果,原有txt文檔內容不變。

mark

  • 查找並替換
  • [ ] sed 's@/sbin/nologin@123@g'

mark

  • 調換兩個字符串的順序

#sed -r 's/([^:]+):(.*):([^:]+)/\3:\2:\1/' // s是替換,^:非冒號字符串,.*非特殊符號的字符串,這裏會遍歷到隨後一個冒號前。轉義字符,替換成\3,\2,\1的形式

mark

  • 刪除文檔中全部的英文字母
[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::/://
  • 在全部的行內容前面加上aaa:
  • [ ] head test.txt |sed -r 's/(.*)/aaa:\1/' 這裏\1或在&均可以表示字符串前面. mark
相關文章
相關標籤/搜索