sed
匹配字符串
sed -n '/root/'p 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 '/r.t/'p test.txt
[root@localhost sed]# sed -n '/r.t/'p test.txt
operator:x:11:0:operator:/root:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
-
sed -n '/r*t/'p test.tx
[root@localhost sed]# sed -n '/r*t/'p test.txt
root:x:0:0:root:/root:/bin/bash
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
operator:x:11:0:operator:/root:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/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 polkitd:/:/sbin/nologin
tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
-
sed -nr '/o+t/'p test.txt
-r选项类似 grep -E
[root@localhost sed]# sed -n '/o+t/'p test.txt
[root@localhost sed]#
[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
-
sed -nr '/root|bus/'p test.txt
[root@localhost sed]# sed -nr '/root|bus/'p test.txt
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
systemd-bus-proxy:x:999:997:systemd Bus Proxy:/:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
[root@localhost sed]#
-
sed -n '2'p test.txt
匹配第二行
[root@localhost sed]# sed -n '2'p test.txt
bin:x:1:1:bin:/bin:/sbin/nologin
匹配第2到5行
[root@localhost sed]# sed -n '2,5'p test.txt
bin:x:1:1:bin:/bin:/sbin/nologin
fsfh:23:rao:fsdfafsaf:fffas
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
[root@localhost sed]#
匹配从第二行到最后一行,用$符号
[root@localhost sed]# sed -n '2,$'p test.txt
bin:x:1:1:bin:/bin:/sbin/nologin
fsfh:23:rao:fsdfafsaf:fffas
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
fdsafa:r>o:ksldjflkasdjfla:qe112312
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 polkitd:/:/sbin/nologin
tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/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:997:995::/var/lib/chrony:/sbin/nologin
user1:x:1000:1007::/hooome/user1:/bin/bash
user2:x:1001:1008::/hoooome/user2:/bin/bash
user3:x:1005:1000::/hoome/user3:/bin/bash
[root@localhost sed]#
-
sed -e '1'p -e '/bus/'p -n test.txt
同时匹配行数和关键词,还可以后面继续-e 增加匹配条件
[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
[root@localhost sed]#
-
sed -n '/bus/'Ip test.txt
加I,可以匹配到大写的。
sed 删除功能
-
sed '1,15'd test.txt
删除1到20行,(实际上并没有真正删除,只是不再屏幕显示)
[root@localhost sed]# sed '1,20'd test.txt
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
chrony:x:997:995::/var/lib/chrony:/sbin/nologin
user1:x:1000:1007::/hooome/user1:/bin/bash
user2:x:1001:1008::/hoooome/user2:/bin/bash
user3:x:1005:1000::/hoome/user3:/bin/bash
[root@localhost sed]#
-i 是真正删除,看下删除之前和删除之后的行数
[root@localhost sed]# wc -l test.txt
26 test.txt
[root@localhost sed]# sed -i '1,20' test.txt
sed:-e 表达式 #1,字符 4:遗漏命令
[root@localhost sed]# sed -i '1,20'd test.txt
[root@localhost sed]# wc -l test.txt
6 test.txt
[root@localhost sed]#
sed -i '/user2/'d test.txt也可以删除含有user2的行
sed 替换功能
-
sed '1,10s/root/toor/g' test.txt
s是替换的意思
g是全局替换
[root@localhost sed]# sed '1,10s/root/toor/g' test.txt
toor:x:0:0:toor:/toor:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
fsfh:23:rao:fsdfafsaf:fffas
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
fdsafa:r>o:ksldjflkasdjfla:qe112312
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 polkitd:/:/sbin/nologin
tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/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:997:995::/var/lib/chrony:/sbin/nologin
user1:x:1000:1007::/hooome/user1:/bin/bash
user2:x:1001:1008::/hoooome/user2:/bin/bash
user3:x:1005:1000::/hoome/user3:/bin/bash
[root@localhost sed]#
-
同样适用正则表达式
sed -r '1,10s/ro+/rrr/g' test.txt |head
前面要加 -r
[root@localhost sed]# sed -r '1,10s/ro+/rrr/g' test.txt |head
rrrt:x:0:0:rrrt:/rrrt:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
fsfh:23:rao:fsdfafsaf:fffas
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
[root@localhost sed]#
-
前后顺序替换
下面内容用:分成了三个段,用\1\2\3代表,\3:\2:\1就是把这三个段反过来。
前面用小括号括起来的,后面要用\ \ 表示
[root@localhost sed]# head test.txt |sed -r 's/([^:]+):(.*):([^:]+)/\3:\2:\1/'
/bin/bash:x:0:0:root:/root:root
/sbin/nologin:x:1:1:bin:/bin:bin
fffas:23:rao:fsdfafsaf:fsfh
/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
-
删除所有英文字母
[root@localhost sed]# sed 's/[a-zA-Z]//g' test.txt
::1:1::/://
:23:::
::2:2::/://
::4:7::///://
::5:0::/://
::6:0::/://
::7:0::/://
::8:12::///://
:>::112312
::11:0::/://
::12:100:://://
::14:50: ://://
::99:99::/://
--::999:997: :/://
-::192:192: :/://
::81:81: :/://
::998:996: :/://
::59:59: ://://
::89:89::///://
::74:74:- :///://
::997:995::///://
1::1000:1007:://1://
2::1001:1008:://2://
3::1005:1000:://3://
[root@localhost sed]#
-
所有行前面增加字符串
[root@localhost sed]# sed -r 's/(.*)/aaa:&/' test.txt |head
aaa:bin:x:1:1:bin:/bin:/sbin/nologin
aaa:fsfh:23:rao:fsdfafsaf:fffas
aaa:daemon:x:2:2:daemon:/sbin:/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:fdsafa:r>o:ksldjflkasdjfla:qe112312
aaa:operator:x:11:0:operator:/root:/sbin/nologin
[root@localhost sed]#