sed用法
使用sed可以实现grep的大部分功能,而且还可以查找替换。
语法:
sed [options] 'command' filename
sed [options] -f scriptfile filename
Options:
-n:-n选项和p命令一起使用表示打印
-e:等价于--expression,多点编辑,在一行执行多条命令
用sed实现匹配(grep)功能=打印有root字符串的行
[root@cham2 sed]# sed -n '/root/'p test.txt
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
[root@cham2 sed]# sed -nr '/r+o/'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
tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin
chrony:x:997:995::/var/lib/chrony:/sbin/nologin
[root@cham2 sed]# sed -nr '/o*t/'p test.txt
root:x:0:0:root:/root:/bin/bash
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halo1t:/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
忽略大小写(I)
[root@cham2 sed]# sed -n '/nologin/'Ip test.txt
bin:x:1:1:bin:/bin:/sbin/NOLOGIN
daemon:x:2:2:daemon:/sbin:/sbin/nologin
ad11om:x:3:4:ad1om:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spoool/lpd:/sbin/nologin
mail:x:8:12:mail:/var/spoooooool/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 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
打印指定行(p)
[root@cham2 sed]# sed -n '2'p test.txt
bin:x:1:1:bin:/bin:/sbin/NOLOGIN
[root@cham2 sed]# sed -n '2,5'p test.txt
bin:x:1:1:bin:/bin:/sbin/NOLOGIN
222222:r.o:dfdsfdsg
daemon:x:2:2:daemon:/sbin:/sbin/nologin
[root@cham2 sed]# sed -n '15,$'p test.txt
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
-e选项允许在同一行里执行多条命令(把第一行打印出来并匹配bus)
[root@cham2 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@cham2 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
说明 “sed -e '1'p -e '/bus/'p -n test.txt”该命令的意思是:打印第一行,同时也打印出匹配有‘bus’的行。当打印的内容和匹配的内容相同时,会分别显示,即:两个命令同时进行但相对独立。
删除指定行(d)
[root@cham2 sed]# sed '1,10'd test.txt
mail:x:8:12:mail:/var/spoooooool/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 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
[root@cham2 sed]# sed '1,18'd test.txt
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
注:这种方法并不会删除文件的内容,知识打印在屏幕上的内容删除了指定行。那么如何直接删除文件中指定的内容呢?如下:
[root@cham2 sed]# wc -l test.txt
23 test.txt
[root@cham2 sed]# sed -i '1,20'd test.txt
[root@cham2 sed]# wc -l test.txt
3 test.txt
[root@cham2 sed]# cat 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
[root@cham2 sed]# sed -i '/SSH/'d test.txt
[root@cham2 sed]# cat test.txt
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
chrony:x:997:995::/var/lib/chrony:/sbin/nologin
查找替换(s) 类似于vim中的查找替换
[root@cham2 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
222222:r.o:dfdsfdsg
daemon:x:2:2:daemon:/sbin:/sbin/nologin
ad11om:x:3:4:ad1om:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spoool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halo1t:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spoooooool/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 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
此处如果不使用head命令,将会显示test.txt全部内容。
[root@cham2 sed]# sed '1,10s/root/toor/g' test.txt | head -3
toor:x:0:0:toor:/toor:/bin/bash
bin:x:1:1:bin:/bin:/sbin/NOLOGIN
[root@cham2 sed]# cat test.txt | head -3
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/NOLOGIN
替换每行第一个字符串和最后一个字符串的位置:
[root@cham2 sed]# head -3 test.txt
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/NOLOGIN
[root@cham2 sed]# head -3 test.txt |sed -r 's/([^:]+):(.*):([^:]+)/\3:\2:\1/'
/bin/bash:x:0:0:root:/root:root
/sbin/NOLOGIN:x:1:1:bin:/bin:bin
说明:小括号内的字符串可以用反斜杠跟数字组合按顺序来代替。
在每一行行首添加指定字符:
[root@cham2 sed]# head -3 test.txt | sed -r 's/(.*)/AB:\1/'
AB:root:x:0:0:root:/root:/bin/bash
AB:bin:x:1:1:bin:/bin:/sbin/NOLOGIN
[root@cham2 sed]# head -3 test.txt | sed -r 's/(.*)/AB:&/'
AB:root:x:0:0:root:/root:/bin/bash
AB:bin:x:1:1:bin:/bin:/sbin/NOLOGIN
说明:在此&代表前面括号中的内容=“\ 1”
领取专属 10元无门槛券
私享最新 技术干货