Linux sed

1.sed介绍

sed:Stream Editor,流编辑器、行编辑器、逐行编辑

sed将每行内容读入到"内存"中,在内存中进行处理,将结果返回给屏幕,此段内存空间称为模式空间。

sed默认不编辑原文件,仅对模式空间的数据进行处理,处理结束后,将模式空间的内容显示到屏幕

2.sed语法

sed命令的语法格式

sed [option] scripts file1 file2 ...

sed [option] 'AddressCommand' file1 file2 ...

Address:表示对哪些行进行处理

Address表示方法:

1.StartLine,EndLine

1,100

1,$

2.LineNumber

3

3.StartLine,+n

5,+2

4./正则表达式/

/root/

/bash$/

5./REGEX1/,/REGEX2/

第1次被Regex1匹配的行开始,到第1次被Regex2匹配的行中间的所有行

6.StartLine,EndLine!

查询不包括StartLine和EndLine行号的行

Command:操作命令

Command表示方法:

d -->删除符合条件的行

p -->显示符合条件的行

r -->从另一个文件中读文件

w -->将文本写入到一个文件

y -->变换字符

= -->打印文件行号

a\ -->在定位行号之后追加文本信息

i\ -->在定位行号之前插入文本信息

c\ -->用新文本替换定位文本

s -->使用替换模式替换相应模式

option选项:

-n:不再显示模式空间中的内容(默认显示)

-i:直接修改原文件

-e 'AddressCommand' -e 'AddressCommand' 同时执行多个匹配操作

-f FILE:将多个AddressCommand保存至文件中,每行一个AddressCommand,读取该文件中的操作同时执行多个操作

-r:表示使用扩展正则表达式

3.样例

3.1.删除符合条件的行

命令:

sed '1,2d' /etc/fstab

[root@rhel77 ~]# cat /etc/fstab

#
# /etc/fstab
# Created by anaconda on Fri Jun  3 09:41:30 2022
#
# 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=c0d6afed-f0cd-4575-9c5d-ce302451860b /                       xfs     defaults        0 0
UUID=bfca56f5-9ad7-4439-8bcb-39663a13ed57 /boot                   xfs     defaults        0 0
UUID=bd04b731-2fd8-4c92-8620-6981f9d82be0 /home                   xfs     defaults        0 0
/dev/mapper/rhel_rhel77-var /var                    xfs     defaults        0 0
UUID=3c784a4d-e8f2-46a5-a0af-d6d21e0410a5 swap			  swap    defaults        0 0
/dev/cdrom	/mnt	iso9660	defaults	0 0
/dev/data_vg/data_lv /mbox xfs defaults 0 0
[root@rhel77 ~]# sed '1,2d' /etc/fstab
# /etc/fstab
# Created by anaconda on Fri Jun  3 09:41:30 2022
#
# 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=c0d6afed-f0cd-4575-9c5d-ce302451860b /                       xfs     defaults        0 0
UUID=bfca56f5-9ad7-4439-8bcb-39663a13ed57 /boot                   xfs     defaults        0 0
UUID=bd04b731-2fd8-4c92-8620-6981f9d82be0 /home                   xfs     defaults        0 0
/dev/mapper/rhel_rhel77-var /var                    xfs     defaults        0 0
UUID=3c784a4d-e8f2-46a5-a0af-d6d21e0410a5 swap			  swap    defaults        0 0
/dev/cdrom	/mnt	iso9660	defaults	0 0
/dev/data_vg/data_lv /mbox xfs defaults 0 0
[root@rhel77 ~]# 

3.2.删除文件中包含UUID的行

命令:

sed '/UUID/d' /etc/fstab

[root@rhel77 ~]# cat /etc/fstab

#
# /etc/fstab
# Created by anaconda on Fri Jun  3 09:41:30 2022
#
# 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=c0d6afed-f0cd-4575-9c5d-ce302451860b /                       xfs     defaults        0 0
UUID=bfca56f5-9ad7-4439-8bcb-39663a13ed57 /boot                   xfs     defaults        0 0
UUID=bd04b731-2fd8-4c92-8620-6981f9d82be0 /home                   xfs     defaults        0 0
/dev/mapper/rhel_rhel77-var /var                    xfs     defaults        0 0
UUID=3c784a4d-e8f2-46a5-a0af-d6d21e0410a5 swap			  swap    defaults        0 0
/dev/cdrom	/mnt	iso9660	defaults	0 0
/dev/data_vg/data_lv /mbox xfs defaults 0 0
[root@rhel77 ~]# sed '/UUID/d' /etc/fstab

#
# /etc/fstab
# Created by anaconda on Fri Jun  3 09:41:30 2022
#
# 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/rhel_rhel77-var /var                    xfs     defaults        0 0
/dev/cdrom	/mnt	iso9660	defaults	0 0
/dev/data_vg/data_lv /mbox xfs defaults 0 0
[root@rhel77 ~]# 

3.3.删除文件第1行及其后2行

命令:

sed '1,+2d' /etc/fstab

[root@rhel77 ~]# cat /etc/fstab

#
# /etc/fstab
# Created by anaconda on Fri Jun  3 09:41:30 2022
#
# 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=c0d6afed-f0cd-4575-9c5d-ce302451860b /                       xfs     defaults        0 0
UUID=bfca56f5-9ad7-4439-8bcb-39663a13ed57 /boot                   xfs     defaults        0 0
UUID=bd04b731-2fd8-4c92-8620-6981f9d82be0 /home                   xfs     defaults        0 0
/dev/mapper/rhel_rhel77-var /var                    xfs     defaults        0 0
UUID=3c784a4d-e8f2-46a5-a0af-d6d21e0410a5 swap			  swap    defaults        0 0
/dev/cdrom	/mnt	iso9660	defaults	0 0
/dev/data_vg/data_lv /mbox xfs defaults 0 0
[root@rhel77 ~]# sed '1,+2d' /etc/fstab
# Created by anaconda on Fri Jun  3 09:41:30 2022
#
# 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=c0d6afed-f0cd-4575-9c5d-ce302451860b /                       xfs     defaults        0 0
UUID=bfca56f5-9ad7-4439-8bcb-39663a13ed57 /boot                   xfs     defaults        0 0
UUID=bd04b731-2fd8-4c92-8620-6981f9d82be0 /home                   xfs     defaults        0 0
/dev/mapper/rhel_rhel77-var /var                    xfs     defaults        0 0
UUID=3c784a4d-e8f2-46a5-a0af-d6d21e0410a5 swap			  swap    defaults        0 0
/dev/cdrom	/mnt	iso9660	defaults	0 0
/dev/data_vg/data_lv /mbox xfs defaults 0 0
[root@rhel77 ~]# 

3.4.删除文件第1行和最后1行

命令:

sed '1d;$d' /etc/fstab

[root@rhel77 ~]# cat /etc/fstab

#
# /etc/fstab
# Created by anaconda on Fri Jun  3 09:41:30 2022
#
# 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=c0d6afed-f0cd-4575-9c5d-ce302451860b /                       xfs     defaults        0 0
UUID=bfca56f5-9ad7-4439-8bcb-39663a13ed57 /boot                   xfs     defaults        0 0
UUID=bd04b731-2fd8-4c92-8620-6981f9d82be0 /home                   xfs     defaults        0 0
/dev/mapper/rhel_rhel77-var /var                    xfs     defaults        0 0
UUID=3c784a4d-e8f2-46a5-a0af-d6d21e0410a5 swap			  swap    defaults        0 0
/dev/cdrom	/mnt	iso9660	defaults	0 0
/dev/data_vg/data_lv /mbox xfs defaults 0 0
[root@rhel77 ~]# sed '1d;$d' /etc/fstab
#
# /etc/fstab
# Created by anaconda on Fri Jun  3 09:41:30 2022
#
# 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=c0d6afed-f0cd-4575-9c5d-ce302451860b /                       xfs     defaults        0 0
UUID=bfca56f5-9ad7-4439-8bcb-39663a13ed57 /boot                   xfs     defaults        0 0
UUID=bd04b731-2fd8-4c92-8620-6981f9d82be0 /home                   xfs     defaults        0 0
/dev/mapper/rhel_rhel77-var /var                    xfs     defaults        0 0
UUID=3c784a4d-e8f2-46a5-a0af-d6d21e0410a5 swap			  swap    defaults        0 0
/dev/cdrom	/mnt	iso9660	defaults	0 0
[root@rhel77 ~]# 

3.5.删除文件以#开头的行

命令:

sed '/^#/d' /etc/fstab

[root@rhel77 ~]# cat /etc/fstab

#
# /etc/fstab
# Created by anaconda on Fri Jun  3 09:41:30 2022
#
# 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=c0d6afed-f0cd-4575-9c5d-ce302451860b /                       xfs     defaults        0 0
UUID=bfca56f5-9ad7-4439-8bcb-39663a13ed57 /boot                   xfs     defaults        0 0
UUID=bd04b731-2fd8-4c92-8620-6981f9d82be0 /home                   xfs     defaults        0 0
/dev/mapper/rhel_rhel77-var /var                    xfs     defaults        0 0
UUID=3c784a4d-e8f2-46a5-a0af-d6d21e0410a5 swap			  swap    defaults        0 0
/dev/cdrom	/mnt	iso9660	defaults	0 0
/dev/data_vg/data_lv /mbox xfs defaults 0 0
[root@rhel77 ~]# sed '/^#/d' /etc/fstab

UUID=c0d6afed-f0cd-4575-9c5d-ce302451860b /                       xfs     defaults        0 0
UUID=bfca56f5-9ad7-4439-8bcb-39663a13ed57 /boot                   xfs     defaults        0 0
UUID=bd04b731-2fd8-4c92-8620-6981f9d82be0 /home                   xfs     defaults        0 0
/dev/mapper/rhel_rhel77-var /var                    xfs     defaults        0 0
UUID=3c784a4d-e8f2-46a5-a0af-d6d21e0410a5 swap			  swap    defaults        0 0
/dev/cdrom	/mnt	iso9660	defaults	0 0
/dev/data_vg/data_lv /mbox xfs defaults 0 0
[root@rhel77 ~]# 

3.6.删除匹配UUID行至最后1行

命令:

sed '/^UUID/,$d' /etc/fstab

[root@rhel77 ~]# cat /etc/fstab

#
# /etc/fstab
# Created by anaconda on Fri Jun  3 09:41:30 2022
#
# 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=c0d6afed-f0cd-4575-9c5d-ce302451860b /                       xfs     defaults        0 0
UUID=bfca56f5-9ad7-4439-8bcb-39663a13ed57 /boot                   xfs     defaults        0 0
UUID=bd04b731-2fd8-4c92-8620-6981f9d82be0 /home                   xfs     defaults        0 0
/dev/mapper/rhel_rhel77-var /var                    xfs     defaults        0 0
UUID=3c784a4d-e8f2-46a5-a0af-d6d21e0410a5 swap			  swap    defaults        0 0
/dev/cdrom	/mnt	iso9660	defaults	0 0
/dev/data_vg/data_lv /mbox xfs defaults 0 0
[root@rhel77 ~]# sed '/^UUID/,$d' /etc/fstab

#
# /etc/fstab
# Created by anaconda on Fri Jun  3 09:41:30 2022
#
# 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@rhel77 ~]# 

3.7.打印文件中的第1行和最后1行

命令:

sed -n '1p;$p' /etc/fstab

[root@rhel77 ~]# cat /etc/fstab

#
# /etc/fstab
# Created by anaconda on Fri Jun  3 09:41:30 2022
#
# 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=c0d6afed-f0cd-4575-9c5d-ce302451860b /                       xfs     defaults        0 0
UUID=bfca56f5-9ad7-4439-8bcb-39663a13ed57 /boot                   xfs     defaults        0 0
UUID=bd04b731-2fd8-4c92-8620-6981f9d82be0 /home                   xfs     defaults        0 0
/dev/mapper/rhel_rhel77-var /var                    xfs     defaults        0 0
UUID=3c784a4d-e8f2-46a5-a0af-d6d21e0410a5 swap			  swap    defaults        0 0
/dev/cdrom	/mnt	iso9660	defaults	0 0
/dev/data_vg/data_lv /mbox xfs defaults 0 0
[root@rhel77 ~]# sed -n '1p;$p' /etc/fstab

/dev/data_vg/data_lv /mbox xfs defaults 0 0
[root@rhel77 ~]# 

3.8.在符合条件的文件行后追加新行

命令:

sed '/^#/a \# hello world' /etc/fstab

[root@rhel77 ~]# cat /etc/fstab

#
# /etc/fstab
# Created by anaconda on Fri Jun  3 09:41:30 2022
#
# 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=c0d6afed-f0cd-4575-9c5d-ce302451860b /                       xfs     defaults        0 0
UUID=bfca56f5-9ad7-4439-8bcb-39663a13ed57 /boot                   xfs     defaults        0 0
UUID=bd04b731-2fd8-4c92-8620-6981f9d82be0 /home                   xfs     defaults        0 0
/dev/mapper/rhel_rhel77-var /var                    xfs     defaults        0 0
UUID=3c784a4d-e8f2-46a5-a0af-d6d21e0410a5 swap			  swap    defaults        0 0
/dev/cdrom	/mnt	iso9660	defaults	0 0
/dev/data_vg/data_lv /mbox xfs defaults 0 0
[root@rhel77 ~]# sed '/^#/a \# hello world' /etc/fstab

#
# hello world
# /etc/fstab
# hello world
# Created by anaconda on Fri Jun  3 09:41:30 2022
# hello world
#
# hello world
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# hello world
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
# hello world
#
# hello world
UUID=c0d6afed-f0cd-4575-9c5d-ce302451860b /                       xfs     defaults        0 0
UUID=bfca56f5-9ad7-4439-8bcb-39663a13ed57 /boot                   xfs     defaults        0 0
UUID=bd04b731-2fd8-4c92-8620-6981f9d82be0 /home                   xfs     defaults        0 0
/dev/mapper/rhel_rhel77-var /var                    xfs     defaults        0 0
UUID=3c784a4d-e8f2-46a5-a0af-d6d21e0410a5 swap			  swap    defaults        0 0
/dev/cdrom	/mnt	iso9660	defaults	0 0
/dev/data_vg/data_lv /mbox xfs defaults 0 0
[root@rhel77 ~]# 

3.9.在符合条件的文件行前追加新行

命令:

sed '1i \#hello world' /etc/fstab

[root@rhel77 ~]# cat /etc/fstab

#
# /etc/fstab
# Created by anaconda on Fri Jun  3 09:41:30 2022
#
# 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=c0d6afed-f0cd-4575-9c5d-ce302451860b /                       xfs     defaults        0 0
UUID=bfca56f5-9ad7-4439-8bcb-39663a13ed57 /boot                   xfs     defaults        0 0
UUID=bd04b731-2fd8-4c92-8620-6981f9d82be0 /home                   xfs     defaults        0 0
/dev/mapper/rhel_rhel77-var /var                    xfs     defaults        0 0
UUID=3c784a4d-e8f2-46a5-a0af-d6d21e0410a5 swap			  swap    defaults        0 0
/dev/cdrom	/mnt	iso9660	defaults	0 0
/dev/data_vg/data_lv /mbox xfs defaults 0 0
[root@rhel77 ~]# sed '1i \#hello world' /etc/fstab
#hello world

#
# /etc/fstab
# Created by anaconda on Fri Jun  3 09:41:30 2022
#
# 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=c0d6afed-f0cd-4575-9c5d-ce302451860b /                       xfs     defaults        0 0
UUID=bfca56f5-9ad7-4439-8bcb-39663a13ed57 /boot                   xfs     defaults        0 0
UUID=bd04b731-2fd8-4c92-8620-6981f9d82be0 /home                   xfs     defaults        0 0
/dev/mapper/rhel_rhel77-var /var                    xfs     defaults        0 0
UUID=3c784a4d-e8f2-46a5-a0af-d6d21e0410a5 swap			  swap    defaults        0 0
/dev/cdrom	/mnt	iso9660	defaults	0 0
/dev/data_vg/data_lv /mbox xfs defaults 0 0
[root@rhel77 ~]# 

3.10.替换指定文件行的内容

命令:

sed '$c \End of File' /etc/fstab

[root@rhel77 ~]# cat /etc/fstab

#
# /etc/fstab
# Created by anaconda on Fri Jun  3 09:41:30 2022
#
# 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=c0d6afed-f0cd-4575-9c5d-ce302451860b /                       xfs     defaults        0 0
UUID=bfca56f5-9ad7-4439-8bcb-39663a13ed57 /boot                   xfs     defaults        0 0
UUID=bd04b731-2fd8-4c92-8620-6981f9d82be0 /home                   xfs     defaults        0 0
/dev/mapper/rhel_rhel77-var /var                    xfs     defaults        0 0
UUID=3c784a4d-e8f2-46a5-a0af-d6d21e0410a5 swap			  swap    defaults        0 0
/dev/cdrom	/mnt	iso9660	defaults	0 0
/dev/data_vg/data_lv /mbox xfs defaults 0 0
[root@rhel77 ~]# sed '$c \End of File' /etc/fstab

#
# /etc/fstab
# Created by anaconda on Fri Jun  3 09:41:30 2022
#
# 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=c0d6afed-f0cd-4575-9c5d-ce302451860b /                       xfs     defaults        0 0
UUID=bfca56f5-9ad7-4439-8bcb-39663a13ed57 /boot                   xfs     defaults        0 0
UUID=bd04b731-2fd8-4c92-8620-6981f9d82be0 /home                   xfs     defaults        0 0
/dev/mapper/rhel_rhel77-var /var                    xfs     defaults        0 0
UUID=3c784a4d-e8f2-46a5-a0af-d6d21e0410a5 swap			  swap    defaults        0 0
/dev/cdrom	/mnt	iso9660	defaults	0 0
End of File
[root@rhel77 ~]# 

3.11.综合案例(编辑网卡配置文件-仅测试)

命令:

cd /etc/sysconfig/network-scripts

sed -e '/^BOOT/c \BOOTPROTO=none' -e '$a \IPADDR=1.1.1.1\nNETMASK=255.255.255.0\nGATEWAY=1.1.1.1' ifcfg-ens33

[root@rhel77 network-scripts]# sed -e '/^BOOT/c \BOOTPROTO=none' -e '$a \IPADDR=1.1.1.1\nNETMASK=255.255.255.0\nGATEWAY=1.1.1.1' ifcfg-ens33
TYPE=Ethernet
PROXY_METHOD=none
BROWSER_ONLY=no
BOOTPROTO=none
DEFROUTE=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_FAILURE_FATAL=no
IPV6_ADDR_GEN_MODE=stable-privacy
NAME=ens33
UUID=a85bdd78-9692-4021-909f-3b6d5c3f2eec
DEVICE=ens33
ONBOOT=yes
#IPADDR=192.168.10.110
#PREFIX=24
#GATEWAY=192.168.10.1
IPV6_PRIVACY=no
IPADDR=1.1.1.1
NETMASK=255.255.255.0
GATEWAY=1.1.1.1
[root@rhel77 network-scripts]# 

3.12.替换文件中UUID为uuid

命令:

sed 's/UUID/uuid/g' /etc/fstab

[root@rhel77 ~]# cat /etc/fstab

#
# /etc/fstab
# Created by anaconda on Fri Jun  3 09:41:30 2022
#
# 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=c0d6afed-f0cd-4575-9c5d-ce302451860b /                       xfs     defaults        0 0
UUID=bfca56f5-9ad7-4439-8bcb-39663a13ed57 /boot                   xfs     defaults        0 0
UUID=bd04b731-2fd8-4c92-8620-6981f9d82be0 /home                   xfs     defaults        0 0
/dev/mapper/rhel_rhel77-var /var                    xfs     defaults        0 0
UUID=3c784a4d-e8f2-46a5-a0af-d6d21e0410a5 swap			  swap    defaults        0 0
/dev/cdrom	/mnt	iso9660	defaults	0 0
/dev/data_vg/data_lv /mbox xfs defaults 0 0
[root@rhel77 ~]# sed 's/UUID/uuid/g' /etc/fstab

#
# /etc/fstab
# Created by anaconda on Fri Jun  3 09:41:30 2022
#
# 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=c0d6afed-f0cd-4575-9c5d-ce302451860b /                       xfs     defaults        0 0
uuid=bfca56f5-9ad7-4439-8bcb-39663a13ed57 /boot                   xfs     defaults        0 0
uuid=bd04b731-2fd8-4c92-8620-6981f9d82be0 /home                   xfs     defaults        0 0
/dev/mapper/rhel_rhel77-var /var                    xfs     defaults        0 0
uuid=3c784a4d-e8f2-46a5-a0af-d6d21e0410a5 swap			  swap    defaults        0 0
/dev/cdrom	/mnt	iso9660	defaults	0 0
/dev/data_vg/data_lv /mbox xfs defaults 0 0
[root@rhel77 ~]# 

3.13.在/etc/fstab查找以UUID开头的行,在其后添加字符串ztj

命令:

sed 's/^UUID/&ztj/g' /etc/fstab

[root@rhel77 ~]# cat /etc/fstab

#
# /etc/fstab
# Created by anaconda on Fri Jun  3 09:41:30 2022
#
# 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=c0d6afed-f0cd-4575-9c5d-ce302451860b /                       xfs     defaults        0 0
UUID=bfca56f5-9ad7-4439-8bcb-39663a13ed57 /boot                   xfs     defaults        0 0
UUID=bd04b731-2fd8-4c92-8620-6981f9d82be0 /home                   xfs     defaults        0 0
/dev/mapper/rhel_rhel77-var /var                    xfs     defaults        0 0
UUID=3c784a4d-e8f2-46a5-a0af-d6d21e0410a5 swap			  swap    defaults        0 0
/dev/cdrom	/mnt	iso9660	defaults	0 0
/dev/data_vg/data_lv /mbox xfs defaults 0 0
[root@rhel77 ~]# sed 's/^UUID/&ztj/g' /etc/fstab

#
# /etc/fstab
# Created by anaconda on Fri Jun  3 09:41:30 2022
#
# 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
#
UUIDztj=c0d6afed-f0cd-4575-9c5d-ce302451860b /                       xfs     defaults        0 0
UUIDztj=bfca56f5-9ad7-4439-8bcb-39663a13ed57 /boot                   xfs     defaults        0 0
UUIDztj=bd04b731-2fd8-4c92-8620-6981f9d82be0 /home                   xfs     defaults        0 0
/dev/mapper/rhel_rhel77-var /var                    xfs     defaults        0 0
UUIDztj=3c784a4d-e8f2-46a5-a0af-d6d21e0410a5 swap			  swap    defaults        0 0
/dev/cdrom	/mnt	iso9660	defaults	0 0
/dev/data_vg/data_lv /mbox xfs defaults 0 0
[root@rhel77 ~]# 

3.14.在/etc/fstab查找以UUID开头的行,在其行首添加字符串ztj

命令:

sed '/^UUID/s/^/&ztj/' /etc/fstab

[root@rhel77 ~]# cat /etc/fstab

#
# /etc/fstab
# Created by anaconda on Fri Jun  3 09:41:30 2022
#
# 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=c0d6afed-f0cd-4575-9c5d-ce302451860b /                       xfs     defaults        0 0
UUID=bfca56f5-9ad7-4439-8bcb-39663a13ed57 /boot                   xfs     defaults        0 0
UUID=bd04b731-2fd8-4c92-8620-6981f9d82be0 /home                   xfs     defaults        0 0
/dev/mapper/rhel_rhel77-var /var                    xfs     defaults        0 0
UUID=3c784a4d-e8f2-46a5-a0af-d6d21e0410a5 swap			  swap    defaults        0 0
/dev/cdrom	/mnt	iso9660	defaults	0 0
/dev/data_vg/data_lv /mbox xfs defaults 0 0
[root@rhel77 ~]# sed '/^UUID/s/^/&ztj/' /etc/fstab

#
# /etc/fstab
# Created by anaconda on Fri Jun  3 09:41:30 2022
#
# 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
#
ztjUUID=c0d6afed-f0cd-4575-9c5d-ce302451860b /                       xfs     defaults        0 0
ztjUUID=bfca56f5-9ad7-4439-8bcb-39663a13ed57 /boot                   xfs     defaults        0 0
ztjUUID=bd04b731-2fd8-4c92-8620-6981f9d82be0 /home                   xfs     defaults        0 0
/dev/mapper/rhel_rhel77-var /var                    xfs     defaults        0 0
ztjUUID=3c784a4d-e8f2-46a5-a0af-d6d21e0410a5 swap			  swap    defaults        0 0
/dev/cdrom	/mnt	iso9660	defaults	0 0
/dev/data_vg/data_lv /mbox xfs defaults 0 0
[root@rhel77 ~]# 

3.15.多个sed命令组合

命令:

sed -e '/^UUID/s/^/&1./;s/0$/&./g' /etc/fstab

[root@rhel77 ~]# cat /etc/fstab

#
# /etc/fstab
# Created by anaconda on Fri Jun  3 09:41:30 2022
#
# 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=c0d6afed-f0cd-4575-9c5d-ce302451860b /                       xfs     defaults        0 0
UUID=bfca56f5-9ad7-4439-8bcb-39663a13ed57 /boot                   xfs     defaults        0 0
UUID=bd04b731-2fd8-4c92-8620-6981f9d82be0 /home                   xfs     defaults        0 0
/dev/mapper/rhel_rhel77-var /var                    xfs     defaults        0 0
UUID=3c784a4d-e8f2-46a5-a0af-d6d21e0410a5 swap			  swap    defaults        0 0
/dev/cdrom	/mnt	iso9660	defaults	0 0
/dev/data_vg/data_lv /mbox xfs defaults 0 0
[root@rhel77 ~]# sed  -e  '/^UUID/s/^/&1./;s/0$/&./g'  /etc/fstab

#
# /etc/fstab
# Created by anaconda on Fri Jun  3 09:41:30 2022
#
# 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
#
1.UUID=c0d6afed-f0cd-4575-9c5d-ce302451860b /                       xfs     defaults        0 0.
1.UUID=bfca56f5-9ad7-4439-8bcb-39663a13ed57 /boot                   xfs     defaults        0 0.
1.UUID=bd04b731-2fd8-4c92-8620-6981f9d82be0 /home                   xfs     defaults        0 0.
/dev/mapper/rhel_rhel77-var /var                    xfs     defaults        0 0.
1.UUID=3c784a4d-e8f2-46a5-a0af-d6d21e0410a5 swap			  swap    defaults        0 0.
/dev/cdrom	/mnt	iso9660	defaults	0 0.
/dev/data_vg/data_lv /mbox xfs defaults 0 0.
[root@rhel77 ~]# 

3.16.sed读取系统变量,变量替换

命令:

variable=ztj

sed "s/UUID/$variable/g" /etc/fstab

[root@rhel77 ~]# variable=ztj
[root@rhel77 ~]# sed "s/UUID/$variable/g" /etc/fstab

#
# /etc/fstab
# Created by anaconda on Fri Jun  3 09:41:30 2022
#
# 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
#
ztj=c0d6afed-f0cd-4575-9c5d-ce302451860b /                       xfs     defaults        0 0
ztj=bfca56f5-9ad7-4439-8bcb-39663a13ed57 /boot                   xfs     defaults        0 0
ztj=bd04b731-2fd8-4c92-8620-6981f9d82be0 /home                   xfs     defaults        0 0
/dev/mapper/rhel_rhel77-var /var                    xfs     defaults        0 0
ztj=3c784a4d-e8f2-46a5-a0af-d6d21e0410a5 swap			  swap    defaults        0 0
/dev/cdrom	/mnt	iso9660	defaults	0 0
/dev/data_vg/data_lv /mbox xfs defaults 0 0
[root@rhel77 ~]# 

3.17.修改文件中不包含UUID的行,然后将其行uuid值改成ztj

命令:

sed '/UUID/!s/ztj/uuid/g' fstab

[root@rhel77 ~]# cat fstab

#
# /etc/fstab
# Created by anaconda on Fri Jun  3 09:41:30 2022
#
# 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
#
ztj=c0d6afed-f0cd-4575-9c5d-ce302451860b /                       xfs     defaults        0 0
ztj=bfca56f5-9ad7-4439-8bcb-39663a13ed57 /boot                   xfs     defaults        0 0
ztj=bd04b731-2fd8-4c92-8620-6981f9d82be0 /home                   xfs     defaults        0 0
/dev/mapper/rhel_rhel77-var /var                    xfs     defaults        0 0
ztj=3c784a4d-e8f2-46a5-a0af-d6d21e0410a5 swap			  swap    defaults        0 0
/dev/cdrom	/mnt	iso9660	defaults	0 0
/dev/data_vg/data_lv /mbox xfs defaults 0 0
[root@rhel77 ~]# sed  '/UUID/!s/ztj/uuid/g' fstab 

#
# /etc/fstab
# Created by anaconda on Fri Jun  3 09:41:30 2022
#
# 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=c0d6afed-f0cd-4575-9c5d-ce302451860b /                       xfs     defaults        0 0
uuid=bfca56f5-9ad7-4439-8bcb-39663a13ed57 /boot                   xfs     defaults        0 0
uuid=bd04b731-2fd8-4c92-8620-6981f9d82be0 /home                   xfs     defaults        0 0
/dev/mapper/rhel_rhel77-var /var                    xfs     defaults        0 0
uuid=3c784a4d-e8f2-46a5-a0af-d6d21e0410a5 swap			  swap    defaults        0 0
/dev/cdrom	/mnt	iso9660	defaults	0 0
/dev/data_vg/data_lv /mbox xfs defaults 0 0
[root@rhel77 ~]# 

3.18.修改文件中包含ztj的行,然后将其行ztj值改成uuid

命令:

sed '/ztj/s/ztj/uuid/gi' fstab

g:替换每一行所有的字符

i:忽略大小写

[root@rhel77 ~]# cat fstab

#
# /etc/fstab
# Created by anaconda on Fri Jun  3 09:41:30 2022
#
# 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
#
ztj=c0d6afed-f0cd-4575-9c5d-ce302451860b /                       xfs     defaults        0 0
ztj=bfca56f5-9ad7-4439-8bcb-39663a13ed57 /boot                   xfs     defaults        0 0
ztj=bd04b731-2fd8-4c92-8620-6981f9d82be0 /home                   xfs     defaults        0 0
/dev/mapper/rhel_rhel77-var /var                    xfs     defaults        0 0
ztj=3c784a4d-e8f2-46a5-a0af-d6d21e0410a5 swap			  swap    defaults        0 0
/dev/cdrom	/mnt	iso9660	defaults	0 0
/dev/data_vg/data_lv /mbox xfs defaults 0 0
[root@rhel77 ~]# sed '/ztj/s/ztj/uuid/gi' fstab

#
# /etc/fstab
# Created by anaconda on Fri Jun  3 09:41:30 2022
#
# 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=c0d6afed-f0cd-4575-9c5d-ce302451860b /                       xfs     defaults        0 0
uuid=bfca56f5-9ad7-4439-8bcb-39663a13ed57 /boot                   xfs     defaults        0 0
uuid=bd04b731-2fd8-4c92-8620-6981f9d82be0 /home                   xfs     defaults        0 0
/dev/mapper/rhel_rhel77-var /var                    xfs     defaults        0 0
uuid=3c784a4d-e8f2-46a5-a0af-d6d21e0410a5 swap			  swap    defaults        0 0
/dev/cdrom	/mnt	iso9660	defaults	0 0
/dev/data_vg/data_lv /mbox xfs defaults 0 0
[root@rhel77 ~]# 

3.19.合并文件上下两行

命令:

sed 'N;s/\n/ /' fatab

[root@rhel77 ~]# sed 'N;s/\n/  /' fstab
  #
# /etc/fstab  # Created by anaconda on Fri Jun  3 09:41:30 2022
#  # 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  #
ztj=c0d6afed-f0cd-4575-9c5d-ce302451860b /                       xfs     defaults        0 0  ztj=bfca56f5-9ad7-4439-8bcb-39663a13ed57 /boot                   xfs     defaults        0 0
ztj=bd04b731-2fd8-4c92-8620-6981f9d82be0 /home                   xfs     defaults        0 0  /dev/mapper/rhel_rhel77-var /var                    xfs     defaults        0 0
ztj=3c784a4d-e8f2-46a5-a0af-d6d21e0410a5 swap			  swap    defaults        0 0  /dev/cdrom	/mnt	iso9660	defaults	0 0
/dev/data_vg/data_lv /mbox xfs defaults 0 0
[root@rhel77 ~]# 

3.20.统计文件行数

命令:

sed -n '$=' /etc/fstab

[root@rhel77 ~]# sed -n '$=' /etc/fstab
15
[root@rhel77 ~]# 

3.21.将指定文件的内容添加到符合条件的行的后面

命令:

sed '2r /etc/hosts' /etc/fstab

在/etc/fstab文件的第2行后追加/etc/hosts文件的内容

[root@rhel77 ~]# sed '2r /etc/hosts' /etc/fstab

#
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.10.110	rhel77.boc.com	rhel77
# /etc/fstab
# Created by anaconda on Fri Jun  3 09:41:30 2022
#
# 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=c0d6afed-f0cd-4575-9c5d-ce302451860b /                       xfs     defaults        0 0
UUID=bfca56f5-9ad7-4439-8bcb-39663a13ed57 /boot                   xfs     defaults        0 0
UUID=bd04b731-2fd8-4c92-8620-6981f9d82be0 /home                   xfs     defaults        0 0
/dev/mapper/rhel_rhel77-var /var                    xfs     defaults        0 0
UUID=3c784a4d-e8f2-46a5-a0af-d6d21e0410a5 swap			  swap    defaults        0 0
/dev/cdrom	/mnt	iso9660	defaults	0 0
/dev/data_vg/data_lv /mbox xfs defaults 0 0
[root@rhel77 ~]# 

3.22.将符合条件的内容另存到指定文件中

命令:

sed '/^UUID/w /tmp/ztj.txt' /etc/fstab

cat /tmp/ztj.txt

将/etc/fstab文件以UUID开头的行另存到/tmp/ztj.txt文件中

[root@rhel77 ~]# sed '/^UUID/w /tmp/ztj.txt' /etc/fstab

#
# /etc/fstab
# Created by anaconda on Fri Jun  3 09:41:30 2022
#
# 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=c0d6afed-f0cd-4575-9c5d-ce302451860b /                       xfs     defaults        0 0
UUID=bfca56f5-9ad7-4439-8bcb-39663a13ed57 /boot                   xfs     defaults        0 0
UUID=bd04b731-2fd8-4c92-8620-6981f9d82be0 /home                   xfs     defaults        0 0
/dev/mapper/rhel_rhel77-var /var                    xfs     defaults        0 0
UUID=3c784a4d-e8f2-46a5-a0af-d6d21e0410a5 swap			  swap    defaults        0 0
/dev/cdrom	/mnt	iso9660	defaults	0 0
/dev/data_vg/data_lv /mbox xfs defaults 0 0
[root@rhel77 ~]# cat /tmp/ztj.txt 
UUID=c0d6afed-f0cd-4575-9c5d-ce302451860b /                       xfs     defaults        0 0
UUID=bfca56f5-9ad7-4439-8bcb-39663a13ed57 /boot                   xfs     defaults        0 0
UUID=bd04b731-2fd8-4c92-8620-6981f9d82be0 /home                   xfs     defaults        0 0
UUID=3c784a4d-e8f2-46a5-a0af-d6d21e0410a5 swap			  swap    defaults        0 0
[root@rhel77 ~]# 
相关推荐
mit6.8245 分钟前
Ubuntu 系统下性能剖析工具: perf
linux·运维·ubuntu
鹏大师运维6 分钟前
聊聊开源的虚拟化平台--PVE
linux·开源·虚拟化·虚拟机·pve·存储·nfs
watermelonoops13 分钟前
Windows安装Ubuntu,Deepin三系统启动问题(XXX has invalid signature 您需要先加载内核)
linux·运维·ubuntu·deepin
不惑_17 分钟前
小白入门 · 腾讯云轻量服务器部署 Hadoop 3.3.6
服务器·hadoop·腾讯云
阿甘知识库36 分钟前
宝塔面板跨服务器数据同步教程:双机备份零停机
android·运维·服务器·备份·同步·宝塔面板·建站
滴水之功1 小时前
VMware OpenWrt怎么桥接模式联网
linux·openwrt
saynaihe1 小时前
安全地使用 Docker 和 Systemctl 部署 Kafka 的综合指南
运维·安全·docker·容器·kafka
ldinvicible1 小时前
How to run Flutter on an Embedded Device
linux
YRr YRr2 小时前
解决Ubuntu 20.04上编译OpenCV 3.2时遇到的stdlib.h缺失错误
linux·opencv·ubuntu
认真学习的小雅兰.2 小时前
如何在Ubuntu上利用Docker和Cpolar实现Excalidraw公网访问高效绘图——“cpolar内网穿透”
linux·ubuntu·docker