1 基本用法
sed [选项参数] ‘command’ filename
2 选项参数说明
选项参数 | 功能 |
---|---|
-e | sed后可以跟多个-e,每个-e指定一个操作。 |
-i | 改变原文件内容 |
3 命令功能描述
命令 | 功能描述 |
---|---|
a | 新增,a的后面可以接字串,在下一行出现 |
d | 删除 |
s | 查找并替换 |
4 具体应用
以Linux中/etc/fstab文件为例,原文件内容为:
#
# /etc/fstab
# Created by anaconda on Wed Jul 15 04:09:58 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
#
UUID=07e7998a-d3a0-4a24-ab2d-6a03890a5a59 / ext4 defaults 1 1
UUID=1cb1b78c-e34f-4bd8-a1af-78db8d8f7a67 /boot ext4 defaults 1 2
UUID=3abe975e-2549-4fb1-9492-964dae991be9 swap swap defaults 0 0
tmpfs /dev/shm tmpfs defaults 0 0
devpts /dev/pts devpts gid=5,mode=620 0 0
sysfs /sys sysfs defaults 0 0
proc /proc proc defaults 0 0
4.1 删除行
- 删除第2-4行
[root@hadoop101 home]# sed "2,4d" /etc/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
#
UUID=07e7998a-d3a0-4a24-ab2d-6a03890a5a59 / ext4 defaults 1 1
UUID=1cb1b78c-e34f-4bd8-a1af-78db8d8f7a67 /boot ext4 defaults 1 2
UUID=3abe975e-2549-4fb1-9492-964dae991be9 swap swap defaults 0 0
tmpfs /dev/shm tmpfs defaults 0 0
devpts /dev/pts devpts gid=5,mode=620 0 0
sysfs /sys sysfs defaults 0 0
proc /proc proc defaults 0 0
- 删除以UUID开头的行
[root@hadoop101 home]# sed "/^UUID/d" /etc/fstab
#
# /etc/fstab
# Created by anaconda on Wed Jul 15 04:09:58 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
#
tmpfs /dev/shm tmpfs defaults 0 0
devpts /dev/pts devpts gid=5,mode=620 0 0
sysfs /sys sysfs defaults 0 0
proc /proc proc defaults 0 0
- 删除空行
[root@hadoop101 home]# sed "/^$/d" /etc/fstab
#
# /etc/fstab
# Created by anaconda on Wed Jul 15 04:09:58 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
#
UUID=07e7998a-d3a0-4a24-ab2d-6a03890a5a59 / ext4 defaults 1 1
UUID=1cb1b78c-e34f-4bd8-a1af-78db8d8f7a67 /boot ext4 defaults 1 2
UUID=3abe975e-2549-4fb1-9492-964dae991be9 swap swap defaults 0 0
tmpfs /dev/shm tmpfs defaults 0 0
devpts /dev/pts devpts gid=5,mode=620 0 0
sysfs /sys sysfs defaults 0 0
proc /proc proc defaults 0 0
4.2 添加行
- 在第2行后新增helloworld行
[root@hadoop101 home]# sed "2a helloword" /etc/fstab
#
helloword
# /etc/fstab
# Created by anaconda on Wed Jul 15 04:09:58 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
#
UUID=07e7998a-d3a0-4a24-ab2d-6a03890a5a59 / ext4 defaults 1 1
UUID=1cb1b78c-e34f-4bd8-a1af-78db8d8f7a67 /boot ext4 defaults 1 2
UUID=3abe975e-2549-4fb1-9492-964dae991be9 swap swap defaults 0 0
tmpfs /dev/shm tmpfs defaults 0 0
devpts /dev/pts devpts gid=5,mode=620 0 0
sysfs /sys sysfs defaults 0 0
proc /proc proc defaults 0 0
4.3 替换内容
- 将文件中的#替换成*(/g代表全局替换的意思)
[root@hadoop101 home]# sed "s/#/*/g" /etc/fstab
*
* /etc/fstab
* Created by anaconda on Wed Jul 15 04:09:58 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
*
UUID=07e7998a-d3a0-4a24-ab2d-6a03890a5a59 / ext4 defaults 1 1
UUID=1cb1b78c-e34f-4bd8-a1af-78db8d8f7a67 /boot ext4 defaults 1 2
UUID=3abe975e-2549-4fb1-9492-964dae991be9 swap swap defaults 0 0
tmpfs /dev/shm tmpfs defaults 0 0
devpts /dev/pts devpts gid=5,mode=620 0 0
sysfs /sys sysfs defaults 0 0
proc /proc proc defaults 0 0
- 将windows文件中的\r回车符去掉
sed -i "s/\r//g"