1、sed是一个文本流的行编辑器工具
用法:
sed [option]…‘script’ inputfile…
获取帮助:
1)man sed
2)sed --help
选项:
-n:不输出模式中的内容至屏幕;(常用)
-e:多点编辑;
-f/PATH/TO/SCRIPT_FILE:从指定文件中读取编辑脚本;
-r:支持使用扩展正则表达式;
-i:保留对文件的编辑;(慎用)
地址定界:
(1)不给地址:对全文进行处理;
(2)单地址:
#:指定的行;
/pattern/:被此处模式所能够匹配到的每一行;
(3)地址范围:
#,# (给定的行号之间)
#,+# (在前一个行的基础上加多少行)
/pat1/,/pat2/
#,/pat1/
编辑命令:
d:删除
p:显示模式空间中的内容
a \‘text’:在行后面追加文本;支持使用\n实现多行追加;
i \‘text’:在行前面追加文本;支持使用\n实现多行插入;
c \‘text’:替换行为单行或多行文本;
w /path/to/somefile:保存模式空间中匹配到的行至指定文件中;
r /path/from/somefile:读取指定文件的文本流至模式空间中匹配到的行的行后;
=:为模式空间中的行打印行号;
!:取反条件;
s///:支持使用其他分隔符,s@@@,s###;
替换标记:
g:行内全局替换;
p:显示替换成功的行;
w /PATH/TO/SOMEFILE:将替换成功的结果保存至指定文件中;
2、示例:
1)删除/etc/fstab文件中所有以#开头的行
[root@www ~]# cat /etc/fstab
#
# /etc/fstab
# Created by anaconda on Mon Dec 31 05:54:54 2018
#
# 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
#
/dev/mapper/centos-root / xfs defaults 0 0
UUID=63d09c18-e20a-485d-b49f-8a4698c03fcd /boot xfs defaults 0 0
/dev/mapper/centos-swap swap swap defaults 0 0
[root@www ~]#
[root@www ~]# sed '/^#/d' /etc/fstab
/dev/mapper/centos-root / xfs defaults 0 0
UUID=63d09c18-e20a-485d-b49f-8a4698c03fcd /boot xfs defaults 0 0
/dev/mapper/centos-swap swap swap defaults 0 0
[root@www ~]#
2)删除/etc/fstab文件中所有的空白行
[root@www ~]# sed '/^$/d' /etc/fstab
#
# /etc/fstab
# Created by anaconda on Mon Dec 31 05:54:54 2018
#
# 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
#
/dev/mapper/centos-root / xfs defaults 0 0
UUID=63d09c18-e20a-485d-b49f-8a4698c03fcd /boot xfs defaults 0 0
/dev/mapper/centos-swap swap swap defaults 0 0
[root@www ~]#
3)删除/etc/fstab文件中前五行
[root@www ~]# nl /etc/fstab
1 #
2 # /etc/fstab
3 # Created by anaconda on Mon Dec 31 05:54:54 2018
4 #
5 # Accessible filesystems, by reference, are maintained under '/dev/disk'
6 # See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
7 #
8 /dev/mapper/centos-root / xfs defaults 0 0
9 UUID=63d09c18-e20a-485d-b49f-8a4698c03fcd /boot xfs defaults 0 0
10 /dev/mapper/centos-swap swap swap defaults 0 0
[root@www ~]#
[root@www ~]# sed '1,5d' /etc/fstab | nl
1 # See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
2 #
3 /dev/mapper/centos-root / xfs defaults 0 0
4 UUID=63d09c18-e20a-485d-b49f-8a4698c03fcd /boot xfs defaults 0 0
5 /dev/mapper/centos-swap swap swap defaults 0 0
[root@www ~]#
4)只打印以UUID开头的行
[root@www ~]# sed -n '/^UUID/p' /etc/fstab
UUID=63d09c18-e20a-485d-b49f-8a4698c03fcd /boot xfs defaults 0 0
[root@www ~]#
5)在每个UUID行后面加上两行自定义的行内容
[root@www ~]# sed '/^UUID/a \hello \nworld' /etc/fstab
#
# /etc/fstab
# Created by anaconda on Mon Dec 31 05:54:54 2018
#
# 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
#
/dev/mapper/centos-root / xfs defaults 0 0
UUID=63d09c18-e20a-485d-b49f-8a4698c03fcd /boot xfs defaults 0 0
hello //此处为新增加的一行
world //第二行
/dev/mapper/centos-swap swap swap defaults 0 0
[root@www ~]#
6)在每个UUID行前面加上两行自定义的行内容
[root@www ~]# sed '/^UUID/i \hello \nworld' /etc/fstab //把a换成i即可在匹配行前面添加内容
#
# /etc/fstab
# Created by anaconda on Mon Dec 31 05:54:54 2018
#
# 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
#
/dev/mapper/centos-root / xfs defaults 0 0
hello
world
UUID=63d09c18-e20a-485d-b49f-8a4698c03fcd /boot xfs defaults 0 0
/dev/mapper/centos-swap swap swap defaults 0 0
[root@www ~]#
7)修改以UUID开头的行内容为hello world
[root@www ~]# sed '/^UUID/c \hello world ' /etc/fstab
#
# /etc/fstab
# Created by anaconda on Mon Dec 31 05:54:54 2018
#
# 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
#
/dev/mapper/centos-root / xfs defaults 0 0
hello world //此处UUID开头的行便成了自定义的内容
/dev/mapper/centos-swap swap swap defaults 0 0
[root@www ~]#
8)将/etc/fstab文件中所有以#开头的行保存到另外一个文件中
[root@www ~]# sed -n '/^#/w /tmp/fstab' /etc/fstab
[root@www ~]# cat /tmp/fstab
#
# /etc/fstab
# Created by anaconda on Mon Dec 31 05:54:54 2018
#
# 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
#
[root@www ~]#
9)打印以UUID行开头的行的行号
[root@www ~]# nl /etc/fstab
1 #
2 # /etc/fstab
3 # Created by anaconda on Mon Dec 31 05:54:54 2018
4 #
5 # Accessible filesystems, by reference, are maintained under '/dev/disk'
6 # See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
7 #
8 /dev/mapper/centos-root / xfs defaults 0 0
9 UUID=63d09c18-e20a-485d-b49f-8a4698c03fcd /boot xfs defaults 0 0
10 /dev/mapper/centos-swap swap swap defaults 0 0
[root@www ~]# sed -n '/^UUID/=' /etc/fstab
9
[root@www ~]#
10)把指定的文件行添加到/etc/fstab文件的第十行下面
[root@www ~]# cat aaa.txt
hello world
l am jyy
[root@www ~]# sed '10r /root/aaa.txt' /etc/fstab |nl
1 #
2 # /etc/fstab
3 # Created by anaconda on Mon Dec 31 05:54:54 2018
4 #
5 # Accessible filesystems, by reference, are maintained under '/dev/disk'
6 # See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
7 #
8 /dev/mapper/centos-root / xfs defaults 0 0
9 UUID=63d09c18-e20a-485d-b49f-8a4698c03fcd /boot xfs defaults 0 0
10 /dev/mapper/centos-swap swap swap defaults 0 0
11 hello world
12 l am jyy
[root@www ~]#
11)删除/etc/fstab文件中除了以UUID开头的行
[root@www ~]# sed '/^UUID/!d' /etc/fstab
UUID=63d09c18-e20a-485d-b49f-8a4698c03fcd /boot xfs defaults 0 0
[root@www ~]#
12)把/etc/fstab文件中所有的UUID替换为小写的uuid
[root@www ~]# sed 's/UUID/uuid/g' /etc/fstab
#
# /etc/fstab
# Created by anaconda on Mon Dec 31 05:54:54 2018
#
# 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
#
/dev/mapper/centos-root / xfs defaults 0 0
uuid=63d09c18-e20a-485d-b49f-8a4698c03fcd /boot xfs defaults 0 0
/dev/mapper/centos-swap swap swap defaults 0 0
[root@www ~]#
13)把/etc/password文件中所有包含r…t的行后面加上jyy
[root@www ~]# sed 's/r..t/&jyy/g' /etc/passwd
rootjyy:x:0:0:rootjyy:/rootjyy:/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:/rootjyy:/sbin/nologin