1.sed格式:
sed [option]... 'script;script;...' [inputfile...]
sed我如果不写文件,那么他就会对标准输入进行处理,所以既然这样,sed就可以支持标准输入重定向,我们可以利用管道,把一些命令的执行结果传给sed来处理.
2.1)
[root@centos7 data]# sed '' abc <-键盘输入 abc <-屏幕输出 ^C
输什么显示什么,sed命令如果什么都不输的话,默认就会打印出来,所以sed命令相当于内置了一个自动打印功能.
2)那么
[root@centos7 data]# sed '' /etc/fstab # # /etc/fstab # Created by anaconda on Fri Dec 31 03:13:58 2021 # # Accessible filesystems, by reference, are maintained under '/dev/disk' # See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info # UUID=d77a4daa-8300-4a3d-a5cf-e8f2eab0ec65 / xfs defaults 0 0 UUID=635cfd6d-6ef5-4357-9ef7-f92ca553a808 /boot xfs defaults 0 0 UUID=c8df228e-65c2-4b45-b6ab-937df6de7633 /data xfs defaults 0 0 UUID=56251bad-5f18-4d5b-9288-d9229240c4a0 swap swap defaults 0 0
就是把这个文件的内容打印出来.
3)标准输入管道
[root@centos7 data]# cat /etc/issue | sed '' My hostname is \n Login time is at \t TTY name is \l \S Kernel \r on an \m ACCESS IS PROHIBITED OR LEGAL SANCTIONS WILL BE IMPSOSED!
3.1)不加地址主动输出
[root@centos7 data]# seq 3 1 2 3 [root@centos7 data]# seq 3 | sed 'p' 1 1 2 2 3 3
他把每一行显示了两遍,因为有自动打印,所以加个p就是打印两遍.
2)如果我不想自动打印那就加上-n
[root@centos7 data]# seq 3 | sed -n 'p' 1 2 3
4.我们来加上地址
[root@centos7 data]# seq 10 | sed -n '3p' 3
之前我们想显示第几行很头疼要用head什么tail的,现在直接3p,哈哈!
5.
[root@centos7 data]# sed '/root/p' /etc/passwd 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 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 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:998:User for polkitd:/:/sbin/nologin libstoragemgmt:x:998:996:daemon account for libstoragemgmt:/var/run/lsm:/sbin/nologin colord:x:997:995:User for colord:/var/lib/colord:/sbin/nologin rpc:x:32:32:Rpcbind Daemon:/var/lib/rpcbind:/sbin/nologin saned:x:996:993:SANE scanner daemon user:/usr/share/sane:/sbin/nologin saslauth:x:995:76:Saslauthd user:/run/saslauthd:/sbin/nologin abrt:x:173:173::/etc/abrt:/sbin/nologin setroubleshoot:x:994:991::/var/lib/setroubleshoot:/sbin/nologin rtkit:x:172:172:RealtimeKit:/proc:/sbin/nologin pulse:x:171:171:PulseAudio System Daemon:/var/run/pulse:/sbin/nologin chrony:x:993:988::/var/lib/chrony:/sbin/nologin unbound:x:992:987:Unbound DNS resolver:/etc/unbound:/sbin/nologin radvd:x:75:75:radvd user:/:/sbin/nologin tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin usbmuxd:x:113:113:usbmuxd user:/:/sbin/nologin geoclue:x:991:985:User for geoclue:/var/lib/geoclue:/sbin/nologin qemu:x:107:107:qemu user:/:/sbin/nologin gluster:x:990:984:GlusterFS daemons:/run/gluster:/sbin/nologin gdm:x:42:42::/var/lib/gdm:/sbin/nologin rpcuser:x:29:29:RPC Service User:/var/lib/nfs:/sbin/nologin nfsnobody:x:65534:65534:Anonymous NFS User:/var/lib/nfs:/sbin/nologin gnome-initial-setup:x:989:983::/run/gnome-initial-setup/:/sbin/nologin sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin avahi:x:70:70:Avahi mDNS/DNS-SD Stack:/var/run/avahi-daemon:/sbin/nologin postfix:x:89:89::/var/spool/postfix:/sbin/nologin ntp:x:38:38::/etc/ntp:/sbin/nologin tcpdump:x:72:72::/:/sbin/nologin joshuaw:x:1000:1000:joshuaw,CEO,10086,96110:/home/joshuaw:/bin/bash
这是把passwd中包含有root的行显示出来,当然,因为它有自动打印,会把别的行打印出来,所以要加上-n
[root@centos7 data]# sed -n '/root/p' /etc/passwd root:x:0:0:root:/root:/bin/bash operator:x:11:0:operator:/root:/sbin/nologin
那么它的功能就相当于
[root@centos7 data]# grep 'root' /etc/passwd root:x:0:0:root:/root:/bin/bash operator:x:11:0:operator:/root:/sbin/nologin
6.1)重要的是sed还支持范围
[root@centos7 data]# seq 10 | sed -n '3,6p' 3 4 5 6
2)还可以用+
[root@centos7 data]# seq 10 | sed -n '3,+4p' 3 4 5 6 7
7.还支持第一个正则表达式匹配之后到下一个正则表达式之前所有行:
我们可以找/etc/passwd中b开头的行到f开头的行
[root@centos7 data]# sed -n '/^b/,/^f/p' /etc/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 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
当然,有人说,如果下面还有b开头的行或者f开头的行怎么办?如果还有那就显示,b开头开始显示,一直向下找,看到f就结束,看不到就都给你显示出来。
8.面试:让你显示某一个日志文件中几点几分到几点几分的日志:
用sed然后两个//,//,从几点几分开始再到结束的这个时间
9.当然sed还可以从第几行开始到下一个模式结束:
[root@centos7 data]# sed -n '/^r/,3p' /etc/passwd root:x:0:0:root:/root:/bin/bash bin:x:1:1:bin:/bin:/sbin/nologin daemon:x:2:2:daemon:/sbin:/sbin/nologin rpc:x:32:32:Rpcbind Daemon:/var/lib/rpcbind:/sbin/nologin rtkit:x:172:172:RealtimeKit:/proc:/sbin/nologin radvd:x:75:75:radvd user:/:/sbin/nologin rpcuser:x:29:29:RPC Service User:/var/lib/nfs:/sbin/nologin
从r开头向下到第三行,不过这种用法不常见.
10.另外,我们还支持步进:
[root@centos7 data]# seq 10 | sed -n '1~2p' 1 3 5 7 9
从1开始每次步进两行
11.1)d:删除,把模式空间内容删掉,不在自动打印
[root@centos7 data]# seq 10 | sed '2~2d' 1 3 5 7 9
把偶数给删了,所以看和刚才的
seq 10 | sed -n '1~2p'
是等价的
2)注意:这个删并不是所谓的真删,他只是把显示的时候不显示了,并不会删除文件内容
12.我们如果想真删,就是把文件给改了,有一个选项可以做到,那就是-i(.bak)加.bak是可以先对原文件备份再编辑
[root@centos7 data]# seq 10 > text.txt [root@centos7 data]# cat text.txt 1 2 3 4 5 6 7 8 9 10 [root@centos7 data]# sed -i.bak '2~2d' text.txt [root@centos7 data]# cat text.txt 1 3 5 7 9 [root@centos7 data]# cat text.txt.bak 1 2 3 4 5 6 7 8 9 10
13.a表示追加
1)在第二行后面追加一个haha
[root@centos7 data]# seq 3 | sed '2ahaha' 1 2 haha 3
2)当然
[root@centos7 data]# seq 3 | sed '2a haha' 1 2 haha 3
不起作用
3)如果我就想haha不顶头前面加一个空格
[root@centos7 data]# seq 3 | sed '2a\ haha' 1 2 haha 3
4)还可以加换行
[root@centos7 data]# seq 10 | sed '2a\haha\nhaha' 1 2 haha haha 3 4 5 6 7 8 9 10
相当于加了两行
5)范例:改配置文件追加,在.bashrc加一个别名
[root@centos7 ~]# sed -i '3aalias x=ls' .bashrc [root@centos7 ~]# cat .bashrc # .bashrc # Source global definitions alias x=ls if [ -f /etc/bashrc ]; then . /etc/bashrc fi # Uncomment the following line if you don't like systemctl's auto-paging feature: # export SYSTEMD_PAGER= # User specific aliases and functions
14.能在后面追加,同样也能在前面插入,插入则为i
[root@centos7 data]# seq 10 | sed '2i\ hehe' 1 hehe 2 3 4 5 6 7 8 9 10
在第二行前面插入 hehe
15.当然能前能后,当然也能中间,中间就是替换了,用c
[root@centos7 data]# seq 10 | sed '2c\hehe' 1 hehe 3 4 5 6 7 8 9 10
将第二行替换成hehe
16.1)我们想把/etc/fstab中所有#开头的行都不显示了:
[root@centos7 data]# cat /etc/fstab # # /etc/fstab # Created by anaconda on Fri Dec 31 03:13:58 2021 # # Accessible filesystems, by reference, are maintained under '/dev/disk' # See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info # UUID=d77a4daa-8300-4a3d-a5cf-e8f2eab0ec65 / xfs defaults 0 0 UUID=635cfd6d-6ef5-4357-9ef7-f92ca553a808 /boot xfs defaults 0 0 UUID=c8df228e-65c2-4b45-b6ab-937df6de7633 /data xfs defaults 0 0 UUID=56251bad-5f18-4d5b-9288-d9229240c4a0 swap swap defaults 0 0 [root@centos7 data]# cat /etc/fstab | sed '/^#/d' UUID=d77a4daa-8300-4a3d-a5cf-e8f2eab0ec65 / xfs defaults 0 0 UUID=635cfd6d-6ef5-4357-9ef7-f92ca553a808 /boot xfs defaults 0 0 UUID=c8df228e-65c2-4b45-b6ab-937df6de7633 /data xfs defaults 0 0 UUID=56251bad-5f18-4d5b-9288-d9229240c4a0 swap swap defaults 0 0
当然这个没有改文件,你要想改文件用-i:
[root@centos7 data]# sed -i '/^#/d' /etc/fstab [root@centos7 data]# cat /etc/fstab UUID=d77a4daa-8300-4a3d-a5cf-e8f2eab0ec65 / xfs defaults 0 0 UUID=635cfd6d-6ef5-4357-9ef7-f92ca553a808 /boot xfs defaults 0 0 UUID=c8df228e-65c2-4b45-b6ab-937df6de7633 /data xfs defaults 0 0 UUID=56251bad-5f18-4d5b-9288-d9229240c4a0 swap swap defaults 0 0
2)把U开头的整行替换成haha
[root@centos7 data]# sed -i '/^U/c haha' /etc/fstab [root@centos7 data]# cat /etc/fstab haha haha haha haha
17.我们可以把文件内容读进去用r
[root@centos7 data]# seq 10 | sed '2~2r /etc/issue' 1 2 My hostname is \n Login time is at \t TTY name is \l \S Kernel \r on an \m ACCESS IS PROHIBITED OR LEGAL SANCTIONS WILL BE IMPSOSED! 3 4 My hostname is \n Login time is at \t TTY name is \l \S Kernel \r on an \m ACCESS IS PROHIBITED OR LEGAL SANCTIONS WILL BE IMPSOSED! 5 6 My hostname is \n Login time is at \t TTY name is \l \S Kernel \r on an \m ACCESS IS PROHIBITED OR LEGAL SANCTIONS WILL BE IMPSOSED! 7 8 My hostname is \n Login time is at \t TTY name is \l \S Kernel \r on an \m ACCESS IS PROHIBITED OR LEGAL SANCTIONS WILL BE IMPSOSED! 9 10 My hostname is \n Login time is at \t TTY name is \l \S Kernel \r on an \m ACCESS IS PROHIBITED OR LEGAL SANCTIONS WILL BE IMPSOSED!
每个偶数行后都追加一遍/etc/issue的内容
18.另外我们还可以用w,w是写
[root@centos7 data]# seq 10 | sed '2~2w a.txt' 1 2 3 4 5 6 7 8 9 10 [root@centos7 data]# cat a.txt 2 4 6 8 10
把偶数行挑出来写到文件里去
19.=是加行号
[root@centos7 data]# seq 10 | sed '2=' 1 2 2 3 4 5 6 7 8 9 10
实际上就是在第二行前面加一个行号
换个文件来看:
[root@centos7 data]# sed '2=' /etc/passwd root:x:0:0:root:/root:/bin/bash 2 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:998:User for polkitd:/:/sbin/nologin libstoragemgmt:x:998:996:daemon account for libstoragemgmt:/var/run/lsm:/sbin/nologin colord:x:997:995:User for colord:/var/lib/colord:/sbin/nologin rpc:x:32:32:Rpcbind Daemon:/var/lib/rpcbind:/sbin/nologin saned:x:996:993:SANE scanner daemon user:/usr/share/sane:/sbin/nologin saslauth:x:995:76:Saslauthd user:/run/saslauthd:/sbin/nologin abrt:x:173:173::/etc/abrt:/sbin/nologin setroubleshoot:x:994:991::/var/lib/setroubleshoot:/sbin/nologin rtkit:x:172:172:RealtimeKit:/proc:/sbin/nologin pulse:x:171:171:PulseAudio System Daemon:/var/run/pulse:/sbin/nologin chrony:x:993:988::/var/lib/chrony:/sbin/nologin unbound:x:992:987:Unbound DNS resolver:/etc/unbound:/sbin/nologin radvd:x:75:75:radvd user:/:/sbin/nologin tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin usbmuxd:x:113:113:usbmuxd user:/:/sbin/nologin geoclue:x:991:985:User for geoclue:/var/lib/geoclue:/sbin/nologin qemu:x:107:107:qemu user:/:/sbin/nologin gluster:x:990:984:GlusterFS daemons:/run/gluster:/sbin/nologin gdm:x:42:42::/var/lib/gdm:/sbin/nologin rpcuser:x:29:29:RPC Service User:/var/lib/nfs:/sbin/nologin nfsnobody:x:65534:65534:Anonymous NFS User:/var/lib/nfs:/sbin/nologin gnome-initial-setup:x:989:983::/run/gnome-initial-setup/:/sbin/nologin sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin avahi:x:70:70:Avahi mDNS/DNS-SD Stack:/var/run/avahi-daemon:/sbin/nologin postfix:x:89:89::/var/spool/postfix:/sbin/nologin ntp:x:38:38::/etc/ntp:/sbin/nologin tcpdump:x:72:72::/:/sbin/nologin joshuaw:x:1000:1000:joshuaw,CEO,10086,96110:/home/joshuaw:/bin/bash
20.!是排除,除了这行的意思
[root@centos7 data]# seq 10 | sed -n '1~2!p' 2 4 6 8 10
打印了除了奇数行,就是打印偶数行
21.对于sed来讲,有一个非常重要的一个选项就是搜索替代,这个是sed的一个核心用法,很重要!
语法:
s/pattern/string/修饰符 替代的字符串
[root@centos7 data]# sed 's/root/admin/' /etc/passwd admin: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:/admin:/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:998:User for polkitd:/:/sbin/nologin libstoragemgmt:x:998:996:daemon account for libstoragemgmt:/var/run/lsm:/sbin/nologin colord:x:997:995:User for colord:/var/lib/colord:/sbin/nologin rpc:x:32:32:Rpcbind Daemon:/var/lib/rpcbind:/sbin/nologin saned:x:996:993:SANE scanner daemon user:/usr/share/sane:/sbin/nologin saslauth:x:995:76:Saslauthd user:/run/saslauthd:/sbin/nologin abrt:x:173:173::/etc/abrt:/sbin/nologin setroubleshoot:x:994:991::/var/lib/setroubleshoot:/sbin/nologin rtkit:x:172:172:RealtimeKit:/proc:/sbin/nologin pulse:x:171:171:PulseAudio System Daemon:/var/run/pulse:/sbin/nologin chrony:x:993:988::/var/lib/chrony:/sbin/nologin unbound:x:992:987:Unbound DNS resolver:/etc/unbound:/sbin/nologin radvd:x:75:75:radvd user:/:/sbin/nologin tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin usbmuxd:x:113:113:usbmuxd user:/:/sbin/nologin geoclue:x:991:985:User for geoclue:/var/lib/geoclue:/sbin/nologin qemu:x:107:107:qemu user:/:/sbin/nologin gluster:x:990:984:GlusterFS daemons:/run/gluster:/sbin/nologin gdm:x:42:42::/var/lib/gdm:/sbin/nologin rpcuser:x:29:29:RPC Service User:/var/lib/nfs:/sbin/nologin nfsnobody:x:65534:65534:Anonymous NFS User:/var/lib/nfs:/sbin/nologin gnome-initial-setup:x:989:983::/run/gnome-initial-setup/:/sbin/nologin sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin avahi:x:70:70:Avahi mDNS/DNS-SD Stack:/var/run/avahi-daemon:/sbin/nologin postfix:x:89:89::/var/spool/postfix:/sbin/nologin ntp:x:38:38::/etc/ntp:/sbin/nologin tcpdump:x:72:72::/:/sbin/nologin joshuaw:x:1000:1000:joshuaw,CEO,10086,96110:/home/joshuaw:/bin/bash
当然并没有改文件,发现一行只改第一个root
[root@centos7 data]# sed 's/root/admin/g' /etc/passwd admin:x:0:0:admin:/admin:/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:/admin:/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:998:User for polkitd:/:/sbin/nologin libstoragemgmt:x:998:996:daemon account for libstoragemgmt:/var/run/lsm:/sbin/nologin colord:x:997:995:User for colord:/var/lib/colord:/sbin/nologin rpc:x:32:32:Rpcbind Daemon:/var/lib/rpcbind:/sbin/nologin saned:x:996:993:SANE scanner daemon user:/usr/share/sane:/sbin/nologin saslauth:x:995:76:Saslauthd user:/run/saslauthd:/sbin/nologin abrt:x:173:173::/etc/abrt:/sbin/nologin setroubleshoot:x:994:991::/var/lib/setroubleshoot:/sbin/nologin rtkit:x:172:172:RealtimeKit:/proc:/sbin/nologin pulse:x:171:171:PulseAudio System Daemon:/var/run/pulse:/sbin/nologin chrony:x:993:988::/var/lib/chrony:/sbin/nologin unbound:x:992:987:Unbound DNS resolver:/etc/unbound:/sbin/nologin radvd:x:75:75:radvd user:/:/sbin/nologin tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin usbmuxd:x:113:113:usbmuxd user:/:/sbin/nologin geoclue:x:991:985:User for geoclue:/var/lib/geoclue:/sbin/nologin qemu:x:107:107:qemu user:/:/sbin/nologin gluster:x:990:984:GlusterFS daemons:/run/gluster:/sbin/nologin gdm:x:42:42::/var/lib/gdm:/sbin/nologin rpcuser:x:29:29:RPC Service User:/var/lib/nfs:/sbin/nologin nfsnobody:x:65534:65534:Anonymous NFS User:/var/lib/nfs:/sbin/nologin gnome-initial-setup:x:989:983::/run/gnome-initial-setup/:/sbin/nologin sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin avahi:x:70:70:Avahi mDNS/DNS-SD Stack:/var/run/avahi-daemon:/sbin/nologin postfix:x:89:89::/var/spool/postfix:/sbin/nologin ntp:x:38:38::/etc/ntp:/sbin/nologin tcpdump:x:72:72::/:/sbin/nologin joshuaw:x:1000:1000:joshuaw,CEO,10086,96110:/home/joshuaw:/bin/bash
在其后面加个g,替换全局
22.我想在/etc/passwd中的r..t替换成r..ter
[root@centos7 data]# sed 's/r..t/&er/g' /etc/passwd rooter:x:0:0:rooter:/rooter:/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:/rooter:/sbin/nologin games:x:12:100:games:/usr/games:/sbin/nologin ftp:x:14:50:FTP User:/var/fterp:/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:998:User for polkitd:/:/sbin/nologin libstoragemgmt:x:998:996:daemon account for libstoragemgmt:/var/run/lsm:/sbin/nologin colord:x:997:995:User for colord:/var/lib/colord:/sbin/nologin rpc:x:32:32:Rpcbind Daemon:/var/lib/rpcbind:/sbin/nologin saned:x:996:993:SANE scanner daemon user:/usr/share/sane:/sbin/nologin saslauth:x:995:76:Saslauthd user:/run/saslauthd:/sbin/nologin abrt:x:173:173::/etc/abrt:/sbin/nologin setroubleshoot:x:994:991::/var/lib/setroubleshoot:/sbin/nologin rtkit:x:172:172:RealtimeKit:/proc:/sbin/nologin pulse:x:171:171:PulseAudio System Daemon:/var/run/pulse:/sbin/nologin chrony:x:993:988::/var/lib/chrony:/sbin/nologin unbound:x:992:987:Unbound DNS resolver:/etc/unbound:/sbin/nologin radvd:x:75:75:radvd user:/:/sbin/nologin tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin usbmuxd:x:113:113:usbmuxd user:/:/sbin/nologin geoclue:x:991:985:User for geoclue:/var/lib/geoclue:/sbin/nologin qemu:x:107:107:qemu user:/:/sbin/nologin gluster:x:990:984:GlusterFS daemons:/run/gluster:/sbin/nologin gdm:x:42:42::/var/lib/gdm:/sbin/nologin rpcuser:x:29:29:RPC Service User:/var/lib/nfs:/sbin/nologin nfsnobody:x:65534:65534:Anonymous NFS User:/var/lib/nfs:/sbin/nologin gnome-initial-setup:x:989:983::/run/gnome-initial-setup/:/sbin/nologin sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin avahi:x:70:70:Avahi mDNS/DNS-SD Stack:/var/run/avahi-daemon:/sbin/nologin postfix:x:89:89::/var/spool/postfix:/sbin/nologin ntp:x:38:38::/etc/ntp:/sbin/nologin tcpdump:x:72:72::/:/sbin/nologin joshuaw:x:1000:1000:joshuaw,CEO,10086,96110:/home/joshuaw:/bin/bash
&代表前面搜索的东西是啥,我就是啥
23.当然还可以使用后向引用
1)将xyz123abc替换成xyz
[root@centos7 data]# echo xyz123abc | sed -r 's/(xyz)123abc/\1/' xyz
2)用sed只留ifconfig ech0 的ip
[root@centos7 data]# ifconfig | sed -rn '2s/.*inet ([0-9.]+) .*/\1/p' 192.168.93.128
24.现在我想把/etc/sysconfig/network-scripts/iscfg-eh0的基名或目录名取出来
1)取目录名
[root@centos7 data]# echo /etc/sysconfig/network-scripts/iscfg-eh0 | sed -r 's/^(.*)\/([^\/]+)/\1/' /etc/sysconfig/network-scripts
2)取基名
[root@centos7 data]# echo /etc/sysconfig/network-scripts/iscfg-eh0 | sed -r 's/^(.*)\/([^\/]+)/\2/' iscfg-eh0
25.将/etc/selinux/config中的SELINUX=enforcing改为SELINUX=disabled:
[root@centos7 data]# sed -i -r 's/^(SELINUX=).*/\1disabled/' /etc/selinux/config [root@centos7 data]# cat /etc/selinux/config # This file controls the state of SELinux on the system. # SELINUX= can take one of these three values: # enforcing - SELinux security policy is enforced. # permissive - SELinux prints warnings instead of enforcing. # disabled - No SELinux policy is loaded. SELINUX=disabled # SELINUXTYPE= can take one of three values: # targeted - Targeted processes are protected, # minimum - Modification of targeted policy. Only selected processes are protected. # mls - Multi Level Security protection. SELINUXTYPE=targeted
26.sed还支持多点编辑用-e选项
27.说明:
-ir 不支持
-i -r 支持
-ri 支持
[root@centos7 data]# sed -ir 's/^(SELINUX=).*/\1disabled/' /etc/selinux/config sed: -e expression #1, char 27: invalid reference \1 on `s' command's RHS [root@centos7 data]# sed -i -r 's/^(SELINUX=).*/\1disabled/' /etc/selinux/config
2022-2-4 0:21