Linux sed命令使用详解

1 基本用法

sed [选项参数]  ‘command’ filename

2 选项参数说明

选项参数功能
-esed后可以跟多个-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 删除行

  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
  1. 删除以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
  1. 删除空行
[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 添加行

  1. 在第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 替换内容

  1. 将文件中的#替换成*(/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
  1. 将windows文件中的\r回车符去掉
sed -i "s/\r//g"
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值