首页 > 系统相关 >Linux sed

Linux sed

时间:2023-09-27 12:36:15浏览次数:49  
标签:UUID fstab dev sed defaults Linux xfs rhel77


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  -->变换字符

      =  -->打印文件行号

在定位行号之后追加文本信息

      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 ~]#

标签:UUID,fstab,dev,sed,defaults,Linux,xfs,rhel77
From: https://blog.51cto.com/ztj1216/7622944

相关文章

  • 论文解读:CrossPoint: Self-Supervised Cross-Modal Contrastive Learning for 3D Poin
    CrossPoint:Self-SupervisedCross-ModalContrastiveLearningfor3DPointCloudUnderstanding本文提出一种简单的跨模态3维—2维区域对应模块,分别将点云模态和图像模态提取的特征向量重新投影到一个公共的特征空间中,并基于最大化与模态无关的互信息的思想设计对比学习损失......
  • linux安装tff格式字体(转载)
    记录下Linux系统装字体的步骤。把字体放到/usr/share/fonts下(可以自己在这个目录下新建一个子目录放进去)fc-cache-fv结束。转载自:https://blog.csdn.net/ouyangzhenxin/article/details/82387632......
  • 如何查看Linux机器上docker容器的cpu占用率和磁盘使用率
    一、查看服务器上各个docker容器服务的CPU占用率:dockerstats 二、查看各docker容器的磁盘占用情况:df -h ......
  • 老板必备!Linux服务器上统计监控各用户的活动数据
    BigBrother链接:https://github.com/hhr346/BigBrother突然的一个想法,然后就花了一晚上实施了一下,具体逻辑是通过直接调用Linux服务器上的命令,在服务器上统计各个用户的活动数据,分为登录次数、登陆时间和进程活动次数。具体的实施的三个部分逻辑都很相似,挑一个在线时长来进行具......
  • linux文件上传至百度网盘备份
    一、摘要说明1.工具:百度网盘的python客户端--bypy2.下载方式:通过pip下载3.实现方案:安装pip-->安装bypy-->百度网盘授权-->测试验证-->扩展4.注意事项:使用bypy工具绑定后,由于百度PCSAPI权限限制,程序只能存取百度云端/apps(我的应用数据)/bypy目录下面的文件和目录。5.命令解释:......
  • linux机器设置密码不过期
    在Linux系统中,可以使用以下步骤来修改密码永不过期:1.使用root用户或具有sudo权限的用户登录到Linux系统中。2.打开终端窗口,并使用以下命令来修改用户的密码失效日期(-1表示永不过期):sudochage-M-1username其中,`username`是要修改密码过期策略的用户名。3.输入密码时,终......
  • linux查看命令 more 、less
    Linuxmore命令类似cat,不过会以一页一页的形式显示,更方便使用者逐页阅读,而最基本的指令就是按空白键(space)就往下一页显示,按b键就会往回(back)一页显示,而且还有搜寻字串的功能(与vi相似),使用中的说明文件,请按h。语法more[-dlfpcsu][-num][+/pattern][+linenum][fileNam......
  • 征稿啦!第 18 届「中国 Linux 内核开发者大会」重磅启动
    第十八届中国Linux内核开发者大会(CLK)如期而至。CLK以“自由、协作、创新”为理念,以开源技术的推广和普及为使命,旨在促进Linux内核开发爱好者相互交流,共同进步。经过组委会公开征集承办单位和各意向承办单位的答辩竞选,本届大会的承办方最终确定为OPPO,今年,我们计划于10月28......
  • 在Linux课程中所学01
    今天在大学期间一节Linux课程中,我学习到了一些基本的命令记录一下less命令命令也是对文件或其他输出进行分页显示,可用pageup、pagedown与键盘方向键控制,查找文件内容比more更容易,最后按q键退出。head命令有些配置文件内容很多,但真正需要查看的内容只有前几行,head命令可以查看......
  • 49、linux下/srv /var /tmp的区别
    /srv:用于存储本机或者本服务器提供的服务数据或数据。(用户主动生产的数据、对外提供服务)/var:系统产生不可自动销毁的缓存文件、日志记录。(系统和程序运行后产生的数据、不对外提供服务、只能用户自己手动清理)/tmp:保存使用完毕后可随时销毁的缓存文件。(有可能是有由系统或程序产......