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

[root@rhcsa ~]# sed 's/^[[:space:]]*//' /etc/grub2.cfg

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

[root@rhcsa ~]# sed 's/^#[[:space:]]*//' /etc/fstab/etc/fstab
Created by anaconda on Fri Jun  3 10:02:33 2022Accessible 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/centos-root /                       xfs     defaults        0 0
UUID=6e5263a8-9127-4657-a83b-dff790e2a139 /boot                   xfs     defaults        0 0
/dev/mapper/centos-swap swap                    swap    defaults        0 0
[root@rhcsa ~]#

3、在 grub2.cfg每一行行首增加#号

[root@rhcsa ~]# sed 's/^.*$/#&/' grub2.cfg

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


[root@rhcsa ~]# sed 's/^[^#]/#&/' /etc/fstab#
# /etc/fstab
# Created by anaconda on Fri Jun  3 10:02:33 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/centos-root /                       xfs     defaults        0 0
#UUID=6e5263a8-9127-4657-a83b-dff790e2a139 /boot                   xfs     defaults        0 0
#/dev/mapper/centos-swap swap                    swap    defaults        0 0
[root@rhcsa ~]#

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

方法一:
[root@rhcsa ~]# ifconfig | sed -n '2p' | sed 's/^.*[[:space:]]*inet.*[[:space:]]//'
192.168.188.255
[root@rhcsa ~]#
方法二:
[root@rhcsa ~]# ifconfig | sed -n '2p' | sed 's/^.*[[:space:]]*inet//' | sed 's/[[:space:]]*netmask.*//'192.168.188.128
[root@rhcsa ~]# 

6、关闭本机SELinux的功能

先来查看一下selinux的状态:

[root@rhcsa ~]# vim /etc/sysconfig/selinux
Error detected while processing /root/.vimrc:
line    1:
E492: Not an editor command: [root@ scripts]# cat  ~/.vimrc
Press ENTER or type command to continue
[root@rhcsa ~]# cat /etc/sysconfig/selinux# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
#     enforcing - SELinux security policy is enforced.
#     permissive - SELinux prints warnings instead of enforcing.
#     disabled - No SELinux policy is loaded.
SELINUX=enable
# SELINUXTYPE= can take one of three two values:
#     targeted - Targeted processes are protected,
#     minimum - Modification of targeted policy. Only selected processes are protected.
#     mls - Multi Level Security protection.
SELINUXTYPE=targeted[root@rhcsa ~]#

使用sed改变状态:

[root@rhcsa ~]# sed -i 's/enable/disabled/' /etc/sysconfig/selinux
[root@rhcsa ~]# cat /etc/sysconfig/selinux# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
#     enforcing - SELinux security policy is enforced.
#     permissive - SELinux prints warnings instead of enforcing.
#     disabled - No SELinux policy is loaded.
SELINUX=disabled
# SELINUXTYPE= can take one of three two values:
#     targeted - Targeted processes are protected,
#     minimum - Modification of targeted policy. Only selected processes are protected.
#     mls - Multi Level Security protection.
SELINUXTYPE=targeted[root@rhcsa ~]#

也可以这样修改:

[root@rhcsa ~]# sed -i 's/SELINUX=disabled/SELINUX=enable/' /etc/sysconfig/selinux
[root@rhcsa ~]# cat /etc/sysconfig/selinux# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
#     enforcing - SELinux security policy is enforced.
#     permissive - SELinux prints warnings instead of enforcing.
#     disabled - No SELinux policy is loaded.
SELINUX=enable
# SELINUXTYPE= can take one of three two values:
#     targeted - Targeted processes are protected,
#     minimum - Modification of targeted policy. Only selected processes are protected.
#     mls - Multi Level Security protection.
SELINUXTYPE=targeted

还可以这样修改:

[root@rhcsa ~]# sed '7 c\SELINUX=enable' /etc/sysconfig/selinux# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
#     enforcing - SELinux security policy is enforced.
#     permissive - SELinux prints warnings instead of enforcing.
#     disabled - No SELinux policy is loaded.
SELINUX=enable
# SELINUXTYPE= can take one of three two values:
#     targeted - Targeted processes are protected,
#     minimum - Modification of targeted policy. Only selected processes are protected.
#     mls - Multi Level Security protection.
SELINUXTYPE=targeted[root@rhcsa ~]#

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

a : 默认每行之后进行添加新内容,此处2a表示每两行之后添加新内容;

[root@rhcsa ~]# sed '2a\192.168.188.128 local' /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.188.128 local
[root@rhcsa ~]#

i : 表示在匹配内容之前追加新内容,此处表示在第一行前添加新内容

[root@rhcsa ~]# sed '1i\192.168.188.128 local' /etc/hosts
192.168.188.128 local
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
[root@rhcsa ~]#

shell---sed语句练习相关推荐

  1. linux shell sed awk 命令(2)-awk

    linux shell sed awk 命令(2)-awk awk语法格式: awk [选项] -f program-file [ -- ] file ... 选项: -F fs, --field-s ...

  2. Linux Shell脚本入门教程系列之(十三)Shell分支语句case … esac教程

    本文是Linux Shell脚本系列教程的第(十三)篇,更多Linux Shell教程请看:Linux Shell脚本系列教程 上一篇之 后,分支语句非常实用,基本上高级语言都支持分支语句(pytho ...

  3. Linux Shell脚本语句执行失败,后续语句继续执行的问题

    Linux Shell脚本语句执行失败,后续语句继续执行的问题 问题 解决方法 问题 写了一个自动部署java后端的脚本,大致如下: A=password read -p "请输入密码:&q ...

  4. Shell循环语句(for循环;while循环;until循环)及中断(break和continue)

    Shell循环语句 for循环语句 例题1:批量添加用户 例题2:根据IP地址检查主机状态 while循环语句 例题1 猜价格游戏 例题二:批量添加用户 until循环语句 例题:计算1~50的值 中 ...

  5. linux shell sed i,Linux Shell学习-sed命令详解

    (1).sed介绍 Sed是流编辑器,stream editor,它是一个将一些列编辑命令作用于一批文本文件的理想工具. (2).sed工作原理 Sed是一个非交互式文本编辑器,它可以对文本文件和标准 ...

  6. shell条件语句及循环语句

    if 语句的结构 在 Shell 脚本应用中,if 语句是最为常用的一种流程控制方式,用来根据特定的条件测试结果,分别执行不同的操作(如果--那么--).根据不同的复杂程度,if 语句的选择结构可以分 ...

  7. bash shell sed 获取第一行_老司机给出的关于 shell 脚本的8个建议,必收!

    这八个建议,来源于键者几年来编写 shell 脚本的一些经验和教训.事实上开始写的时候还不止这几条,后来思索再三,去掉几条无关痛痒的,最后剩下八条.毫不夸张地说,每条都是精挑细选的,虽然有几点算是老生 ...

  8. linux shell 循环语句 for while until

    for/do/done Shell脚本的for循环结构和C语言很不一样,它类似于某些编程语言的foreach循环.例如: #! /bin/shfor FRUIT in apple banana pea ...

  9. linux shell编程语句if、case.

    shell学习笔记--if,case shell的控制流结构主要有if语句.for语句.case语句.while语句.until语句这五种,在shell中这些语句的用法有点类似C语言,很容易学会,但也 ...

  10. shell sed过滤器详解

    1. Sed简介 sed 是一种在线编辑器,它一次处理一行内容.处理时,把当前处理的行存储在临时缓冲区中,称为"模式空间"(pattern space),接着用sed命令处理缓冲区 ...

最新文章

  1. 《C++标准程序库》读书笔记(三)
  2. Robot Framework测试用例并行执行
  3. 恶补各种知识(操作系统篇)
  4. Wget CVE-2014-4877:FTP 符号链接任意文件系统访问
  5. ORACLE SGA问题分析
  6. 如何防止用户重复提交表单
  7. undertale人物_传说之下所有人物的照片
  8. 【分享】费曼技巧---以教促学
  9. Linux配置虚拟主机后,只能访问到主页怎么办?
  10. parsley.js自定义验证规则之大小写
  11. Verilog中的specify block和timing check
  12. 央企如何做好数字化转型战略规划
  13. java串口监控数据,怎样监听或者拦截串口上的数据
  14. 用极限定义证明微积分基本定理
  15. 用计算机玩吃鸡,W10电脑系统玩吃鸡游戏会卡顿怎么办
  16. mysql5.7 alter,MySQL 5.7推荐使用ALTER USER修改密码
  17. OSDI 2021 VEGITO 论文阅读
  18. OC与C语言的一些区别
  19. python字符串及其函数
  20. CSDN云IDE 初体验

热门文章

  1. 天津理工大学计算机网络复习
  2. 2021-06-30软件测试day1学习笔记
  3. Postgresql GIN索引
  4. Zilliz 荣获「2021 年优秀开源创新企业」称号
  5. Spring系列:java字符串转long类型
  6. java基础知识大全
  7. 搜狗高速浏览器怎么设置鼠标手势显示动作提示?设置方法是什么
  8. 软件测试面试2017
  9. html table th 一列包含多列,使用TH colspan每列包含多个TD元素
  10. 西乡竖坐司法与止政互动机制