1. 程式人生 > 其它 >【04-04】 刪除/etc/fstab檔案中所有以#開頭,後面至少跟一個空白字元的行的行首的#和空白字元

【04-04】 刪除/etc/fstab檔案中所有以#開頭,後面至少跟一個空白字元的行的行首的#和空白字元

【04-04】刪除/etc/fstab檔案中所有以#開頭,後面至少跟一個空白字元的行的行首的#和空白字元

# 備份檔案到root家目錄做測試
[root@centos-7 ~]# cp /etc/fstab /root/fstab
#檢視該檔案
[root@centos-7 ~]# cat /root/fstab 

#
# /etc/fstab
# Created by anaconda on Thu Mar  7 06:38:37 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=4b499d76-769a-40a0-93dc-4a31a59add28 / ext4 defaults 1 1 #對檔案進行處理 /^# 取得各行已#開頭的行後傳遞給 s進行替換 s/^#// 替換為空後進行對檔案修改 [root@centos-7 ~]# sed -ri.bak '/^#/s/^#//' /root/fstab [root@centos-7 ~]# cat /root/fstab /etc/fstab Created by anaconda on Thu Mar
7 06:38:37 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=4b499d76-769a-40a0-93dc-4a31a59add28 / ext4 defaults 1 1 # 增加一個條件刪除前方空格 [root@centos-7 ~]# sed -ri.bak '/^#/s/^#//;/^ /s/^ //
' /root/fstab [root@centos-7 ~]# cat fstab /etc/fstab Created by anaconda on Thu Mar 7 06:38:37 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=4b499d76-769a-40a0-93dc-4a31a59add28 / ext4 defaults 1 1 #刪除#號行和空行 [root@centos-7 ~]# sed -i.bak '/^# */d;/^$/d' /root/fstab [root@centos-7 ~]# cat fstab UUID=4b499d76-769a-40a0-93dc-4a31a59add28 /