sed可以实现grep的大部分功能,而且还可以查找替换。
[root@centos-01 ~]# ls
1.txt 2.txt anaconda-ks.cfg grep test
[root@centos-01 ~]#
[root@centos-01 ~]#
[root@centos-01 ~]#
[root@centos-01 ~]#
[root@centos-01 ~]# mkdir sed
[root@centos-01 ~]# cd sed
[root@centos-01 sed]# cp ../grep/passwd test.txt
[root@centos-01 sed]# pwd
/root/sed
[root@centos-01 sed]# ls
test.txt
-n
选项和p
命令一起使用表示只打印那些匹配的行
[root@centos-01 sed]# sed '/root/'p -n test.txt
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
[root@centos-01 sed]# sed '/r.t/'p -n test.txt
operator:x:11:0:operator:/root:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
[root@centos-01 sed]# sed -n '/ro\+t/'p test.txt
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
- 不加
\
符号,则需要使用-r
选项
[root@centos-01 sed]# sed -nr '/ro+t/'p test.txt
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
[root@centos-01 sed]# sed -nr '/o{2}/'p test.txt
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@centos-01 sed]# sed -nr '/root|mail/'p test.txt
root:x:0:0:root:/root:/bin/bash
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
- 打印文件指定行
[root@centos-01 sed]# sed -n '2'p test.txt
bin:x:1:1:bin:/bin:/sbin/nologin
[root@centos-01 sed]# sed -n '2,4'p test.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@centos-01 sed]# sed -n '16,$'p 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
NOLogin
-e
选项允许同时执行多条操作,对应内容依次输出
[root@centos-01 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@centos-01 sed]# sed -e '1'p -e '/mail/'p -n test.txt
root:x:0:0:root:/root:/bin/bash
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
I
命令匹配时不区分大小写
[root@centos-01 sed]# sed -n '/system/'p test.txt
systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
[root@centos-01 sed]# sed -n '/system/'Ip test.txt
systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
d
命令删除显示在屏幕上内容的指定行,原本文件并不会执行删除
[root@centos-01 sed]# wc -l test.txt
20 test.txt
[root@centos-01 sed]# sed '1,17'd test.txt
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
chrony:x:998:996::/var/lib/chrony:/sbin/nologin
NOLogin
[root@centos-01 sed]# wc -l test.txt
20 test.txt
-i
直接修改文件内容
[root@centos-01 sed]# cp test.txt test.txt.bak
[root@centos-01 sed]# sed -i '1,17'd test.txt
[root@centos-01 sed]# wc -l test.txt
3 test.txt
[root@centos-01 sed]# cat test.txt
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
chrony:x:998:996::/var/lib/chrony:/sbin/nologin
NOLogin
[root@centos-01 sed]# sed -i '/sshd/'d test.txt
[root@centos-01 sed]# cat test.txt
chrony:x:998:996::/var/lib/chrony:/sbin/nologin
NOLogin
- 替换
[root@centos-01 sed]# cp test.txt.bak test.txt
cp:是否覆盖"test.txt"? y
[root@centos-01 sed]# sed '1,10s/root/ROOT/g' 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
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-network:x:192:192:systemd Network Management:/:/sbin/nologin
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
NOLogin
[root@centos-01 sed]# sed -r '1,10s/ro+/r/g' test.txt |head
rt:x:0:0:rt:/rt:/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:/rt:/sbin/nologin
[root@centos-01 sed]# head 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
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
[root@centos-01 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
/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@centos-01 sed]# head test.txt |sed 's/\/sbin\/nologin/TEST/g'
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:TEST
daemon:x:2:2:daemon:/sbin:TEST
adm:x:3:4:adm:/var/adm:TEST
lp:x:4:7:lp:/var/spool/lpd:TEST
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:TEST
operator:x:11:0:operator:/root:TEST
[root@centos-01 sed]# head test.txt |sed 's@/sbin/nologin@TEST@g'
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:TEST
daemon:x:2:2:daemon:/sbin:TEST
adm:x:3:4:adm:/var/adm:TEST
lp:x:4:7:lp:/var/spool/lpd:TEST
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:TEST
operator:x:11:0:operator:/root:TEST
[root@centos-01 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@centos-01 sed]# head test.txt |sed -r 's/(.*)/#&/'
#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