正則 sed

sed


  • sed 強項在於替換,替換一些指定的字符;
[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]#

▶ -n 不打印無關的行 p 輸出bash

▶若是不加 -n 則會把區配的關鍵字行打印兩次,並列出無關的行ssh

[root@localhost sed]# sed '/root/'p test.txt 
root:x:0:0:root:/root:/bin/bash
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin

▶查找內容爲ot的關鍵字行,可是須要脫義符 \ 例:post

[root@localhost sed]# sed -n '/o\+t/'p test.txt 
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
[root@localhost sed]#

▶要不想使用脫義符 \ 須要在命令中 加 -r 選項code

[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

▶能夠屢次區配字符串

[root@localhost sed]# sed -nr '/o{2}/'p test.txt  ▶區配兩次 o
root:x:0:0:root:/root:/bin/bash
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
[root@localhost sed]#

▶區配 或者it

[root@localhost sed]# sed -nr '/root|bus/'p test.txt  ▶匹配root或者bus
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
[root@localhost sed]#

sed 打印指定的行

[root@localhost sed]# sed -n '2'p test.txt  ▶指定打印第二行
bin:x:1:1:bin:/bin:/sbin/nologin
[root@localhost sed]#

▶能夠指定範圍test

[root@localhost sed]# sed -n '2,5'p test.txt ▶打印 2-5行
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
[root@localhost sed]#
[root@localhost sed]# sed -n '15,$'p test.txt ▶打印15-末行 $表明末行
dbus:x:81:81:System message bus:/:/sbin/nologin
polkitd:x:999:997:User for polkitd:/:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
chrony:x:998:996::/var/lib/chrony:/sbin/nologin
[root@localhost sed]#

  • -e 能夠在一個命令時面有多個表達式
[root@localhost sed]# sed -e '1'p -e '/root/'p -n test.txt ▶打印第一行和區配 root
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]#

表達式重合會打印兩次,由於是兩個不一樣的表達式,只要生效就會打印

  • -I 選項能夠區分大小寫 (大i)
[root@localhost sed]# sed -n '/bus/'Ip test.txt  ▶加上 I 能夠將大小寫的關鍵字一同區配出來
games:x:12:100:games:/usr/games:/sbin/nologinbus
nobody:x:99:99:Nobody:/:/sbin/nologinBUS
dbus:x:81:81:System message bus:/:/sbin/nologin
[root@localhost sed]#

  • -d 刪除指定行
[root@localhost sed]# sed '1,15'd test.txt ▶只是在打印時把匹配以後剩下的打印出來,不實際刪除
polkitd:x:999:997:User for polkitd:/:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
chrony:x:998:996::/var/lib/chrony:/sbin/nologin
[root@localhost sed]# wc -l test.txt 
19 test.txt
[root@localhost sed]#
  • -i 加入 -i 選項纔會實際刪除文件內容
[root@localhost sed]# sed -i '1,15'd test.txt 
[root@localhost sed]# 
[root@localhost sed]# wc -l test.txt 
4 test.txt
[root@localhost sed]#
[root@localhost sed]# sed -i '/root/'d test.txt ▶也能夠指定關鍵字相關行刪除
[root@localhost sed]#

  • s 查找替換
[root@localhost sed]# sed '1,10s'/root/toor/g test.txt ▶s 替換 ,g 全局替換
toor:x:0:0:toor:/toor:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
....會所有打印出來

[root@localhost sed]# head test.txt |sed 's/\/nologin/123/g'▶將文件前10行中的nollogin替換成123
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin123
daemon:x:2:2:daemon:/sbin:/sbin123
adm:x:3:4:adm:/var/adm:/sbin123
lp:x:4:7:lp:/var/spool/lpd:/sbin123
▶注意:/ 之間必定要用 \ 隔開,否則會報錯,或者用@ @, #  #  隔開;

  • 刪除文件內容中的全部英文字母
[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::/://
::8:12::///://
::11:0::/://
[root@localhost sed]#
  • 在全部行前面加上固定字符串
[root@localhost sed]# head test.txt |sed -r 's/(.*)/aaa:&/'
aaa:root:x:0:0:root:/root:/bin/bash
aaa:bin:x:1:1:bin:/bin:/sbin/nologin
aaa:daemon:x:2:2:daemon:/sbin:/sbin/nologin
aaa:adm:x:3:4:adm:/var/adm:/sbin/nologin
aaa:lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
aaa:sync:x:5:0:sync:/sbin:/bin/sync
aaa:shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
aaa:halt:x:7:0:halt:/sbin:/sbin/halt
aaa:mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
aaa:operator:x:11:0:operator:/root:/sbin/nologin
[root@localhost sed]#
相關文章
相關標籤/搜索