正則介紹_sed

10月17日任務centos

9.4/9.5 sedbash

 

sed工具

匹配打印

  • -n 只打印匹配行,否則其餘行也會打印出來
  • p 打印(配合-n使用)
[root@centos7 tmp]# sed -n '/root/'p passwd 
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
  • 忽略大小寫(大寫的i)
# 注意I的位置
[root@centos7 tmp]# sed -n '/castiel/'Ip passwd
castiel:x:1000:1000::/home/castiel:/bin/bash
CASTIEL:X:1000:1000::/HOME/CASTIEL:/BIN/BASH
  • 正則
# ^ 以什麼開頭
[root@localhost ~]# sed -n '/^ro/'p text.txt 
root:x:0:0:root:/root:/bin/bash

# 符號.替代單個字符
[root@localhost ~]# sed -nr '/r..t/'p text.txt 
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin

打印行

  • 打印指定行
[root@localhost ~]# sed -n '2'p text.txt 
bin:x:1:1:bin:/bin:/sbin/nologin
  • 打印一段(連續的幾行)
[root@localhost ~]# sed -n '2,4'p text.txt 
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
  • 所有打印(首尾範圍)
[root@localhost ~]# sed -n '1,$'p text.txt 
root:x:0:0:root:/root:/bin/bash
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
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
nobody:x:99:99:Nobody:/:/sbin/nologin
systemd-bus-proxy:x:999:997:systemd Bus Proxy:/:/sbin/nologin
systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
polkitd:x:998:996:User for 
...

內容刪除

  • d 刪除
# 刪除1-5行,只做用到打印屏幕的內容,文本內容實際上未改變
# 要想做用到內容須要加-i
[root@centos7 tmp]# sed -n '1,5'd test.txt
  • -i 將操做同步到文本
真正刪除文本內1-5行
[root@centos7 tmp]# sed -ni '1,5'd test.txt

內容替換

  • s/範圍、正則等/替換後內容/g (末尾不加g,只替換一行中第一個匹配到的字符串)
特定字符匹配替換(當行首個和所有替換)
# 行首替換
[root@centos7 tmp]# sed -n 's/cas/CSa/'p test.txt
CSatiel:x:1000:1000::/home/castiel:/bin/bash

# 所有替換
[root@localhost ~]# sed -n 's/cas/CAS/g'p /etc/passwd
CAStiel:x:1000:1000::/home/CAStiel:/bin/bash
  • -r 使用正則方式替換內容
# 使用擴展正則+匹配內容
[root@localhost ~]# sed -nr 's/o+t/1/g'p test.txt 
r1:x:0:0:r1:/r1:/bin/bash
operator:x:11:0:operator:/r1:/sbin/nologin
  • 刪除文本內的字符(實質爲將字母替換爲空)
[root@localhost ~]# sed -n 's/[a-zA-Z]//g'p test.txt | head
::0:0::/://
::1:1::/://
::2:2::/://
::3:4:://://
::4:7::///://
::5:0::/://
::6:0::/://
::7:0::/://
::8:12::///://
::11:0::/://

多條件匹配

多個匹配條件(能夠多個,每一個條件前加-e)工具

  • -e '/匹配條件/'
[root@centos7 tmp]# sed -n -e '/root/'p -e '/castiel/'p text.txt
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
castiel:x:1000:1000::/home/castiel:/bin/bash

sed命令補充

  • -i 參數擴展(慎用)
-i[suffix] 或 --in-place=[suffix]
指定suffix能夠在執行時爲操做前的文本建立副本,保存內容
[root@centos7 tmp]# head -n 5 /etc/passwd > test.txt
[root@centos7 tmp]# cat test.txt
root:x:0:0:root:/root:/bin/bash
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@centos7 tmp]# sed -i\.bak '1'd passwd
或
[root@centos7 tmp]# sed --in-place=.bak '1'd passwd

[root@centos7 tmp]# ll
總用量 8
-rw-r--r--. 1 root root 151 10月  3 11:17 passwd
-rw-r--r--. 1 root root 183 10月  3 11:17 passwd.bak
[root@centos7 tmp]# cat passwd
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@centos7 tmp]# cat passwd.bak 
root:x:0:0:root:/root:/bin/bash
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
  • 擴展正則分組 使用()包圍1組內容,後面用\1對應表示,-r參數必須
將最後1列與第1列互換位置
[root@centos7 tmp]# sed -nr 's/([^:]+):(.*):([^:]+)/\3:\2:\1/g'p passwd
/bin/bash:x:0:0:root:/root:root
/sbin/nologin:x:1:1:bin:/bin:bin
/sbin/nologin:x:2:2:daemon:/sbin:daemon
/sbin/nologin:x:3:4:adm:/var/adm:adm
/sbin/nologin:x:4:7:lp:/var/spool/lpd:lp
/bin/sync:x:5:0:sync:/sbin:sync
/sbin/shutdown:x:6:0:shutdown:/sbin:shutdown
/sbin/halt:x:7:0:halt:/sbin:halt
/sbin/nologin:x:8:12:mail:/var/spool/mail:mail
/sbin/nologin:x:11:0:operator:/root:operator
  • & 表明以前()內的匹配值
匹配的每行前加#(能夠用做註釋功能)
[root@centos7 tmp]# cat passwd | sed -r 's/(.*)/#&/g'
#root:x:0:0:root:/root:/bin/bash
#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
#sync:x:5:0:sync:/sbin:/bin/sync
#shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
#halt:x:7:0:halt:/sbin:/sbin/halt
#mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
#operator:x:11:0:operator:/root:/sbin/nologin
相關文章
相關標籤/搜索