shell——sed练习题

1、删除/etc/grub2.conf文件中所有以空白开头的行行首的空白字符

[root@localhost ~]# sed -r 's/^[[:space:]]//' /etc/grub2.cfg
#
# DO NOT EDIT THIS FILE
#
# It is automatically generated by grub2-mkconfig using templates
# from /etc/grub.d and settings from /etc/default/grub
#

### BEGIN /etc/grub.d/00_header ###
set pager=1

if [ -f ${config_directory}/grubenv ]; then
 load_env -f ${config_directory}/grubenv
elif [ -s $prefix/grubenv ]; then
 load_env
fi
if [ "${next_entry}" ] ; then
  set default="${next_entry}"
  set next_entry=
  save_env next_entry
  set boot_once=true
else
  set default="${saved_entry}"
fi

if [ x"${feature_menuentry_id}" = xy ]; then
 menuentry_id_option="--id"
else
 menuentry_id_option=""
fi

2、删除/etc/fstab文件中所有以#开头,后面至少跟一个空白字符的行的行首的#和空白字符

[root@localhost ~]# sed -r "s/^#[[:space:]]*//" /etc/fstab


/etc/fstab
Created by anaconda on Sat Oct 22 12:45:03 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.

After editing this file, run 'systemctl daemon-reload' to update systemd
units generated from this file.

UUID=6beb0f09-220d-4fdd-ad09-3739c16a0c06 /                       xfs     defaults        0 0
UUID=8450c75d-6fc1-4e25-9bc0-d48589712f53 /boot                   xfs     defaults        0 0
UUID=de1476ec-158d-4c6a-a52e-304410038adc none                    swap    defaults        0 0

3、在/root/install.log每一行行首增加#号

[root@localhost ~]# sed -n "s/^.*$/#&/" /root/install.log

4、在/etc/fstab文件中不以#开头的行的行首增加#号

[root@localhost ~]# sed -r "s/^[^#]/#&/" /etc/fstab

#
# /etc/fstab
# Created by anaconda on Sat Oct 22 12:45:03 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.
#
# After editing this file, run 'systemctl daemon-reload' to update systemd
# units generated from this file.
#
#UUID=6beb0f09-220d-4fdd-ad09-3739c16a0c06 /                       xfs     defaults        0 0
#UUID=8450c75d-6fc1-4e25-9bc0-d48589712f53 /boot                   xfs     defaults        0 0
#UUID=de1476ec-158d-4c6a-a52e-304410038adc none                    swap    defaults        0 0

5、利用sed 取出ifconfig命令中本机的IPv4地址

[root@localhost ~]# ifconfig | sed -n 2p | sed -r "s/.*inet[[:space:]]*//" | sed -r "s/[[:space:]].*//"
192.168.190.133

6、关闭本机SELinux的功能

[root@localhost ~]# sed -i "s/^SELINUX=enforcing/SELINUX=disabled/" /etc/sysconfig/selinux

7、在/etc/hosts配置文件中添加内容

[root@localhost ~]# sed -i "1 i \hello" /etc/hosts
[root@localhost ~]# cat /etc/hosts
hello
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.190.133 www.zuoye.com
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

SUPER COW

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值