sed练习
-
-
- 1、删除centos7系统/etc/grub2.cfg文件中所有以空白开头的行行首的空白字符
- 2、删除/etc/fstab文件中所有以#开头,后面至少跟一个空白字符的行的行首的#和空白字符
- 3、在centos6系统/root/install.log每一行行首增加#号
- 4、在/etc/fstab文件中不以#开头的行的行首增加#号
- 5、处理/etc/fstab路径,使用sed命令取出其目录名和基名
- 6、利用sed 取出ifconfig命令中本机的IPv4地址
- 7、统计centos安装光盘中Package目录下的所有rpm文件的以.分隔倒数第二个字段的重复次数
- 8、统计/etc/init.d/functions文件中每个单词的出现次数,并排序(用grep和sed两种方法分别实现)
- 9、将文本文件的n和n+1行合并为一行,n为奇数行(sed高级用法)
-
1、删除centos7系统/etc/grub2.cfg文件中所有以空白开头的行行首的空白字符
[root@centos8 ~]#tail /etc/grub2.cfg
# the 'exec tail' line above.
### END /etc/grub.d/40_custom ###
### BEGIN /etc/grub.d/41_custom ###
if [ -f ${config_directory}/custom.cfg ]; then
source ${config_directory}/custom.cfg
elif [ -z "${config_directory}" -a -f $prefix/custom.cfg ]; then
source $prefix/custom.cfg;
fi
### END /etc/grub.d/41_custom ###
[root@centos8 ~]#sed -r 's/^[[:space:]]+(.*)/\1/' /etc/grub2.cfg |tail
# the 'exec tail' line above.
### END /etc/grub.d/40_custom ###
### BEGIN /etc/grub.d/41_custom ###
if [ -f ${config_directory}/custom.cfg ]; then
source ${config_directory}/custom.cfg
elif [ -z "${config_directory}" -a -f $prefix/custom.cfg ]; then
source $prefix/custom.cfg;
fi
### END /etc/grub.d/41_custom ###
2、删除/etc/fstab文件中所有以#开头,后面至少跟一个空白字符的行的行首的#和空白字符
这题目太绕了
我的理解是,对以#[:sapce:]+开头的行处理,并删除#[:sapce:]+
[root@centos8 ~]#cat /etc/fstab
#
# /etc/fstab
# Created by anaconda on Wed May 12 09:10:42 2021
#
# 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.
#
# After editing this file, run 'systemctl daemon-reload' to update systemd
# units generated from this file.
#
UUID=eacaaab6-1589-4ef4-aeb8-a5fc1ec176a4 /