sed工具的使用
作用:编辑修改文件
特性:
1、逐行处理文本工具
2、默认情况,不修改源文件
工作流程:
将符合的条件的内容逐行加载到模式空间, 在内存中对数据进行修改, 默认将模式 空间中的数据重定向到屏幕
sed工具使用格式
# sed [options] ‘script’ file1 file2
# sed [options] ‘lineCommand’ file1 file2
line常用写法
表示对哪些行进行操作
1)行号
10 【文件第十行]】
2)起始行号, 终止行号
2,5 1,10 1,$ 【起始行,终止行】
3)/正则表达式/
/正则表达式/, +3 【符合正则表达式加其后三行】
/正则表达式1/, /正则表达式2/
基本命令操作
d 删除整行
1.删除第三行
[root@localhost ~]# cat -n ./fstab
1
2 #
3 # /etc/fstab
4 # Created by anaconda on Sat Aug 15 19:25:23 2020
5 #
6 # Accessible filesystems, by reference, are maintained under '/dev/disk'
7 # See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
8 #
9 /dev/mapper/centos-root / xfs defaults 0 0
10 UUID=3b747cea-665b-400c-ad83-b9f50c6fd030 /boot xfs defaults 0 0
11 /dev/mapper/centos-swap swap swap defaults 0 0
[root@localhost ~]# sed '3d' ./fstab
#
# Created by anaconda on Sat Aug 15 19:25:23 2020
#
# 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=3b747cea-665b-400c-ad83-b9f50c6fd030 /boot xfs defaults 0 0
/dev/mapper/centos-swap swap swap defaults 0 0
2.删除前五行
[root@localhost ~]# sed '1,5d' ./fstab
# 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=3b747cea-665b-400c-ad83-b9f50c6fd030 /boot xfs defaults 0 0
/dev/mapper/centos-swap swap swap defaults 0 0
[root@localhost ~]#
3.删除最后一行
[root@localhost ~]# sed '$d' ./fstab
#
# /etc/fstab
# Created by anaconda on Sat Aug 15 19:25:23 2020
#
# 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=3b747cea-665b-400c-ad83-b9f50c6fd030 /boot xfs defaults 0 0
[root@localhost ~]#
4.删除#号开头的行
[root@localhost ~]# sed '/^#/d' ./fstab
/dev/mapper/centos-root / xfs defaults 0 0
UUID=3b747cea-665b-400c-ad83-b9f50c6fd030 /boot xfs defaults 0 0
/dev/mapper/centos-swap swap swap defaults 0 0
[root@localhost ~]#
5.配合管道使用
[root@localhost ~]# df -H
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/centos-root 46G 8.3G 38G 19% /
devtmpfs 946M 0 946M 0% /dev
tmpfs 957M 0 957M 0% /dev/shm
tmpfs 957M 9.0M 948M 1% /run
tmpfs 957M 0 957M 0% /sys/fs/cgroup
/dev/sda1 521M 131M 391M 26% /boot
tmpfs 192M 0 192M 0% /run/user/0
[root@localhost ~]# df -H|sed '1d'
/dev/mapper/centos-root 46G 8.3G 38G 19% /
devtmpfs 946M 0 946M 0% /dev
tmpfs 957M 0 957M 0% /dev/shm
tmpfs 957M 9.0M 948M 1% /run
tmpfs 957M 0 957M 0% /sys/fs/cgroup
/dev/sda1 521M 131M 391M 26% /boot
tmpfs 192M 0 192M 0% /run/user/0
[root@localhost ~]#
p 显示符合条件的行
1.显示以#号开头的行
[root@localhost ~]# sed -n '/^#/'p ./fstab
#
# /etc/fstab
# Created by anaconda on Sat Aug 15 19:25:23 2020
#
# 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
#
2.显示前三行
[root@localhost ~]# sed -n '1,3p' ./fstab
#
# /etc/fstab
a … 向文件追加内容
1.向文件末尾行追加内容
[root@localhost ~]# sed '$a kkkkkjkkkk' ./fstab
#
# /etc/fstab
# Created by anaconda on Sat Aug 15 19:25:23 2020
#
# 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=3b747cea-665b-400c-ad83-b9f50c6fd030 /boot xfs defaults 0 0
/dev/mapper/centos-swap swap swap defaults 0 0
kkkkkjkkkk
[root@localhost ~]#
2.向以#号开头的行追加内容
[root@localhost ~]# sed '/^#/a kkkkkjkkkk' ./fstab
#
kkkkkjkkkk
# /etc/fstab
kkkkkjkkkk
# Created by anaconda on Sat Aug 15 19:25:23 2020
kkkkkjkkkk
#
kkkkkjkkkk
# Accessible filesystems, by reference, are maintained under '/dev/disk'
kkkkkjkkkk
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
kkkkkjkkkk
#
kkkkkjkkkk
/dev/mapper/centos-root / xfs defaults 0 0
UUID=3b747cea-665b-400c-ad83-b9f50c6fd030 /boot xfs defaults 0 0
/dev/mapper/centos-swap swap swap defaults 0 0
[root@localhost ~]#
i … 插入内容
最后一行插入内容
[root@localhost ~]# sed '$i kkkkkjkkkk' ./fstab
#
# /etc/fstab
# Created by anaconda on Sat Aug 15 19:25:23 2020
#
# 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=3b747cea-665b-400c-ad83-b9f50c6fd030 /boot xfs defaults 0 0
kkkkkjkkkk
/dev/mapper/centos-swap swap swap defaults 0 0
[root@localhost ~]#
c … 整行替换
1.将以#开头的整行替换成目的内容
[root@localhost ~]# sed '/^#/c kkkkkjkkkk' ./fstab
kkkkkjkkkk
kkkkkjkkkk
kkkkkjkkkk
kkkkkjkkkk
kkkkkjkkkk
kkkkkjkkkk
kkkkkjkkkk
/dev/mapper/centos-root / xfs defaults 0 0
UUID=3b747cea-665b-400c-ad83-b9f50c6fd030 /boot xfs defaults 0 0
/dev/mapper/centos-swap swap swap defaults 0 0
[root@localhost ~]#
r 文件名称 读取文件
[root@localhost ~]# vim ceshi
sjgahsjkgkaisjdhngfakjsdhg
[root@localhost ~]# cat ./fstab
#
# /etc/fstab
# Created by anaconda on Sat Aug 15 19:25:23 2020
#
# 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=3b747cea-665b-400c-ad83-b9f50c6fd030 /boot xfs defaults 0 0
/dev/mapper/centos-swap swap swap defaults 0 0
[root@localhost ~]# sed '$r ./ceshi' ./fstab
#
# /etc/fstab
# Created by anaconda on Sat Aug 15 19:25:23 2020
#
# 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=3b747cea-665b-400c-ad83-b9f50c6fd030 /boot xfs defaults 0 0
/dev/mapper/centos-swap swap swap defaults 0 0
gfdhsjgahsjkgkaisjdhngfakjsdhg
[root@localhost ~]#
w 文件名称 另存
将符合条件的内容写入到目标文件内
[root@localhost ~]# sed -n '/^#/w ./ceshi' ./fstab
[root@localhost ~]#
[root@localhost ~]#
[root@localhost ~]# cat ./ceshi
#
# /etc/fstab
# Created by anaconda on Sat Aug 15 19:25:23 2020
#
# 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@localhost ~]#
= 统计行数
[root@localhost ~]# sed '1,$=' ./fstab
1
2
#
3
# /etc/fstab
4
# Created by anaconda on Sat Aug 15 19:25:23 2020
5
#
6
# Accessible filesystems, by reference, are maintained under '/dev/disk'
7
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
8
#
9
/dev/mapper/centos-root / xfs defaults 0 0
10
UUID=3b747cea-665b-400c-ad83-b9f50c6fd030 /boot xfs defaults 0 0
11
/dev/mapper/centos-swap swap swap defaults 0 0
[root@localhost ~]#
n 跳行处理
[root@localhost ~]# sed -n '1,$p' ./fstab
#
# /etc/fstab
# Created by anaconda on Sat Aug 15 19:25:23 2020
#
# 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=3b747cea-665b-400c-ad83-b9f50c6fd030 /boot xfs defaults 0 0
/dev/mapper/centos-swap swap swap defaults 0 0
[root@localhost ~]# sed -n '1,$p;n' ./fstab
# /etc/fstab
#
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
/dev/mapper/centos-root / xfs defaults 0 0
/dev/mapper/centos-swap swap swap defaults 0 0
[root@localhost ~]# sed -n 'n;p' ./fstab
#
# Created by anaconda on Sat Aug 15 19:25:23 2020
# Accessible filesystems, by reference, are maintained under '/dev/disk'
#
UUID=3b747cea-665b-400c-ad83-b9f50c6fd030 /boot xfs defaults 0 0
[root@localhost ~]#
s 替换操作
语法
s/old/new/[修饰符]
old支持使用正则表达式
修饰符:
g: 替换所有
i: 忽略大小写
1.替换目标内容
[root@localhost ~]# sed 's/UUID/数字/' ./fstab
#
# /etc/fstab
# Created by anaconda on Sat Aug 15 19:25:23 2020
#
# 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
数字=3b747cea-665b-400c-ad83-b9f50c6fd030 /boot xfs defaults 0 0
/dev/mapper/centos-swap swap swap defaults 0 0
[root@localhost ~]#
常用选项
选项 | 作用 |
---|---|
-n | 取消默认显示模式空间中的数据 |
-i | 修改源文件 |
-e | 按多条件修改 1 [root@localhost ~]# sed ‐e ‘/^#/d’ ‐e ‘/^$/d’ /etc/fstab |
-f | 从文件中读取操作 1 [root@localhost ~]# sed ‐f /tmp/file01 /etc/fstab |
-r | 支持扩展正则表达式 |
–follow-symlinks | 修改软链接文件必加 |