命令格式
sed [OPTION] … ‘script’ [FILE] …
script:
地址定界编辑命令
常用选项
-n:不输出模式空间中的内容至屏幕
-e:多点编辑,当同时使用多个命令时,可以使用此选项
-f /PATH/TO/SED_SCRIPT_FILE
每行一个编辑命令
-r,—regrxp-extend:支持使用扩展的正则表达式
-i :直接编辑原文件(慎用)
地址定界
1)不给地址:对全文进行处理
2)单地址:
#:指定行
/pattern/:被此模式匹配到的每一行
3)地址范围
#,#:
#,+#:
#,/pattern/
/pattern1/,/pattern2/
4) 步进地址:~
1~2 :奇数行
2~2 : 偶数行
编辑命令
下面例子所使用的文件为test和/etc/fstab文件,原文本为
[root@localhost ~]# cat test
1a
2b
3c
4d
5e
6f
7g
8h
9i
10j
[root@localhost ~]# cat /etc/fstab
#
# /etc/fstab
# Created by anaconda on Fri Apr 3 06:52:45 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=352015d4-63ed-40a1-bdb3-f32c30d7f565 /boot xfs defaults 0 0
/dev/mapper/centos-swap swap swap defaults 0 0
d: 删除
[root@localhost ~]# sed ‘1,5d’ test #删除test文件中1到5行
[root@localhost ~]# sed ‘1~2d’ test #删除奇数行
[root@localhost ~]# sed ‘2~2d’ test #删除偶数行
[root@localhost ~]# sed '1,5d' test
6f
7g
8h
9i
10j
[root@localhost ~]# sed '1~2d' test
2b
4d
6f
8h
10j
[root@localhost ~]# sed '2~2d' test
1a
3c
5e
7g
9i
p:打印模式空间中的内容
[root@localhost ~]# sed -n ‘1p’ test #显示第一行,-n在这里表示不现实没有被模式匹配到的行
[root@localhost ~]# sed -n '1p' test
1a
a、i:追加和插入
a \text:在匹配到的行后面追加文本”\text”,支持使用\n,实现多行追加
i \text:在行前面插入文件”\text”
[root@localhost ~]# sed ‘/^UUID/a #new line’ /etc/fstab
[root@localhost ~]# sed ‘3i #new line’ /etc/fstab
[root@localhost ~]# sed '/^UUID/a \#new line' /etc/fstab
#
# /etc/fstab
# Created by anaconda on Fri Apr 3 06:52:45 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=352015d4-63ed-40a1-bdb3-f32c30d7f565 /boot xfs defaults 0 0
#new line
/dev/mapper/centos-swap swap swap defaults 0 0
[root@localhost ~]# sed '3i \#new line' /etc/fstab
#
#new line
# /etc/fstab
# Created by anaconda on Fri Apr 3 06:52:45 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=352015d4-63ed-40a1-bdb3-f32c30d7f565 /boot xfs defaults 0 0
/dev/mapper/centos-swap swap swap defaults 0 0
c:替换
c \text:把匹配到的行替换为此处指定的文本“text”
[root@localhost ~]# sed ‘/^UUID/c #be changed line’ /etc/fstab,将UUID开头的行替换为#be changed line
[root@localhost ~]# sed '/^UUID/c \#be changed line' /etc/fstab
#
# /etc/fstab
# Created by anaconda on Fri Apr 3 06:52:45 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
#be changed line
/dev/mapper/centos-swap swap swap defaults 0 0
w:保存至文件
w /PATH/TO/SOMEFILE 保存模式空间中匹配到的行至指定的文件中
保存所有以非#开头的行到指定/tmp/fstab.new文件中
[root@localhost ~]# sed -n ‘/^[^#]/w /tmp/fstab.new’ /etc/fstab
[root@localhost ~]# sed -n '/^[^#]/w /tmp/fstab.new' /etc/fstab
[root@localhost ~]# cat /tmp/fstab.new
/dev/mapper/centos-root / xfs defaults 0 0
UUID=352015d4-63ed-40a1-bdb3-f32c30d7f565 /boot xfs defaults 0 0
/dev/mapper/centos-swap swap swap defaults 0 0
r:文件合并
r /PATH/TO/SOMEFILE:读取指定文件的内容至当前文件被模式匹配到的行后面,文件合并
[root@localhost ~]# sed ‘/^UUID/r /etc/issue’ /etc/fstab #在所有UUID的行后面插入/etc/issue中的内容
[root@localhost ~]# sed '/^UUID/r /etc/issue' /etc/fstab
#
# /etc/fstab
# Created by anaconda on Fri Apr 3 06:52:45 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=352015d4-63ed-40a1-bdb3-f32c30d7f565 /boot xfs defaults 0 0
\S
Kernel \r on an \m
/dev/mapper/centos-swap swap swap defaults 0 0
= :为模式空间匹配到行打印行号
[root@localhost ~]# sed '1,3=' test
1
1a
2
2b
3
3c
4d
5e
6f
7g
8h
9i
10j
! : 条件取反
eg:sed ‘/^#/!w’ /tmp/fstab.new /etc/fstab
[root@localhost ~]# sed -n ‘/^[^#]/!w /tmp/fstab.new’ /etc/fstab #将#开头的行保存至文件
[root@localhost ~]# sed -n '/^[^#]/!w /tmp/fstab.new' /etc/fstab
[root@localhost ~]# cat /tmp/fstab.new
#
# /etc/fstab
# Created by anaconda on Fri Apr 3 06:52:45 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
#
s///:查找替换
s///:查找替换,其分隔符可以自行指定,常用的有S@@@,s###等
g:全局替换
w:/PATH/TO/SOMEFILE:将替换成功的结果保存至指定文件中
p:显示替换成功的行
[root@localhost ~]# sed ‘s/aa/bereplace/‘ test
[root@localhost ~]# sed ‘s/aa/bereplace/g’ test
[root@localhost ~]# sed -n ‘s/aa/bereplace/p’ test
[root@localhost ~]# sed 's/aa/bereplace/' test
bereplace
bb
cc
bereplace
[root@localhost ~]# sed 's/aa/bereplace/g' test
bereplace
bb
cc
bereplace
[root@localhost ~]# sed -n 's/aa/bereplace/p' test
bereplace
bereplace
练习:
1、删除/boot/grub/grub2.cfg文件中所有以空白字符开头的行的行首的所有空白字符
sed ‘s/^[[:space:]]+//‘ /boot/grub/grub2.cfg
2、删除/etc/fstab文件中所有以#开头的行的行首的#号,以及#后面所有空白字符
sed ‘s/^#[[:space:]]*//‘ /etc/fstab
3、输出一个绝对路径给sed命令,取出其目录,其行为类似dirname
echo “/var/log/messages” | sed ‘s#[^/]+$##’
echo “/var/log/messages/“ | sed ‘s#[^/]+/\?$##’
echo “/var/log/messages” | sed -r ‘s#[^/]+/?$##’
[root@localhost ~]# echo "/var/log/messages" | sed 's#[^/]\+$##'
/var/log/
[root@localhost ~]# echo "/var/log/messages/" | sed 's#[^/]\+/\?$##'
/var/log/
[root@localhost ~]# echo /var/log/messages| sed -r 's#[^/]+/?$##'
/var/log/
高级编辑命令:
h:把模式空间中的内容覆盖至保持空间中
H:把模式空间中的内容追加至保持空间中
g:把保持空间中的内容覆盖至模式空间中
G:把保持空间中的内容追加至模式空间中
X:把模式空间与保持空间中的内容互换
n:覆盖读取匹配到的行的下一行至模式空间中
N:追加读取匹配到的行的下一行至模式空间中
d:删除模式空间中的行
D:删除多行模式空间中的所有行
示例:
sed -n ‘n;p’ FILE 显示偶数行文件内容
sed ‘1!G;h;$!d’ FILE 逆序显示文件内容(过程较复杂)
sed ‘$!d’ FILE :取出最后一行
sed ‘$!N;$!D’ FILE 取出文件最后两行
sed ‘/^$/d;D’ FILE:删除原有的所有空白行,而后为所有的非空白行后添加一行空白行
sed ‘n,d’ FILE 显示奇数行
sed ‘G’ FILE :在原有的每行后方添加一个空白行