1.1.1 删除/etc/fstab文件中所有以#开头的行的行首的# 号及#后面的所有空白字符;
空白字符
空格
多个空格
tab键
此题可以分为两个题目:
- 删除/etc/fstab文件中所有以#开头的行的行首的# 号及#后面的所有空白字符
- 删除/etc/fstab文件中所有以#开头的行的行首的# 号及#后面的连续的空白字符
1.1.1.1 删除/etc/fstab文件中所有以#开头的行的行首的# 号及#后面的所有空白字符
可以分为两步:哪个是条件,哪个是动作
- 第1步找出以#开头的行
- 第2步在删除这一行中的空白字符和行首的井号
awk '/^#/{gsub(/^#|[ \t]+/,"")}1' /etc/fstab
sed -r '/^#/s@^#|[ \t]+@@g' /etc/fstab
演示:
原文内容
[root@oldboyedu59 ~]# cat /etc/fstab
#
# /etc/fstab
# Created by anaconda on Tue Mar 26 11:37:39 2019
#
# 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=1e827748-cc43-4b1d-8bbc-9a85c239782d / xfs defaults 0 0
UUID=29a4ef75-7502-48df-b31c-c7c10264a7c8 /boot xfs defaults 0 0
UUID=60c45f5e-c78d-41fe-aa92-93d10de6def3 swap swap defaults 0 0
awk '/^#/{gsub(/^#|[ \t]+/,"")}1' /etc/fstab
[root@oldboyedu59 ~]# awk '/^#/{gsub(/^#|[ \t]+/,"")}1' /etc/fstab
/etc/fstab
CreatedbyanacondaonTueMar2611:37:392019
Accessiblefilesystems,byreference,aremaintainedunder'/dev/disk'
Seemanpagesfstab(5),findfs(8),mount(8)and/orblkid(8)formoreinfo
UUID=1e827748-cc43-4b1d-8bbc-9a85c239782d / xfs defaults 0 0
UUID=29a4ef75-7502-48df-b31c-c7c10264a7c8 /boot xfs defaults 0 0
UUID=60c45f5e-c78d-41fe-aa92-93d10de6def3 swap swap defaults 0 0
sed -r '/^#/s@^#|[ \t]+@@g' /etc/fstab
[root@oldboyedu59 ~]# sed -r '/^#/s@^#|[ \t]+@@g' /etc/fstab
/etc/fstab
CreatedbyanacondaonTueMar2611:37:392019
Accessiblefilesystems,byreference,aremaintainedunder'/dev/disk'
Seemanpagesfstab(5),findfs(8),mount(8)and/orblkid(8)formoreinfo
UUID=1e827748-cc43-4b1d-8bbc-9a85c239782d / xfs defaults 0 0
UUID=29a4ef75-7502-48df-b31c-c7c10264a7c8 /boot xfs defaults 0 0
UUID=60c45f5e-c78d-41fe-aa92-93d10de6def3 swap swap defaults 0 0
1.1.1.2 删除/etc/fstab文件中所有以#开头的行的行首的# 号及#后面的连续的空白字符
- awk '{gsub(/^#[ \t]+/,"")}1' /etc/fstab
- sed -r 's@^#[ \t]+@@g' /etc/fstab
[root@oldboyedu59 ~]# cat /etc/fstab
#
# /etc/fstab
# Created by anaconda on Tue Mar 26 11:37:39 2019
#
# 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=1e827748-cc43-4b1d-8bbc-9a85c239782d / xfs defaults 0 0
UUID=29a4ef75-7502-48df-b31c-c7c10264a7c8 /boot xfs defaults 0 0
UUID=60c45f5e-c78d-41fe-aa92-93d10de6def3 swap swap defaults 0 0
[root@oldboyedu59 ~]# awk '{gsub(/^#[ \t]+/,"")}1' /etc/fstab
#
/etc/fstab
Created by anaconda on Tue Mar 26 11:37:39 2019
#
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=1e827748-cc43-4b1d-8bbc-9a85c239782d / xfs defaults 0 0
UUID=29a4ef75-7502-48df-b31c-c7c10264a7c8 /boot xfs defaults 0 0
UUID=60c45f5e-c78d-41fe-aa92-93d10de6def3 swap swap defaults 0 0
[root@oldboyedu59 ~]# sed -r 's@^#[ \t]+@@g' /etc/fstab
#
/etc/fstab
Created by anaconda on Tue Mar 26 11:37:39 2019
#
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=1e827748-cc43-4b1d-8bbc-9a85c239782d / xfs defaults 0 0
UUID=29a4ef75-7502-48df-b31c-c7c10264a7c8 /boot xfs defaults 0 0
UUID=60c45f5e-c78d-41fe-aa92-93d10de6def3 swap swap defaults 0 0