zoukankan      html  css  js  c++  java
  • linux shell搜索某个字符串,然后在后面加上字符串?字符串后面插入字符串?sed字符串后面插入字符串?

    需求描述:

      今天在配置nrpe.cfg这个文件,里面有allowed_hosts的IP地址,需要加上监控主机的地址,所以首先要搜索

      到这个地址,然后呢,加上监控主机的地址,考虑通过sed命令来实现

    操作过程

    1.查看原文件

    [root@testvm02 ~]# cat nrpe.cfg 
    allowed_hosts=127.0.0.1

    2.通过sed命令,在后面加上监控端的主机IP

    [root@testvm02 ~]# sed -i 's/allowed_hosts=127.0.0.1/&,192.168.53.25/' nrpe.cfg #通过-i表示直接对文件进行操作s表示替换通过&+字符串,来实现将新的字符串增加到找到得字符串的后面.
    [root@testvm02 ~]# cat nrpe.cfg   #重新查看文件的内容,已经在原来的字符串后面加上了新的字符串(带有逗号的字符串)
    allowed_hosts=127.0.0.1,192.168.53.25

    3.如果有多行,想要同时都进行替换

    [root@testvm02 ~]# cat nrpe.cfg 
    allowed_hosts=127.0.0.1
    allowed_hosts=127.0.0.1
    [root@testvm02 ~]# sed -i 's/allowed_hosts=127.0.0.1/&,192.168.53.25/g' nrpe.cfg   #通过g参数将文档中所有满足的字符串后面都加上新的字符串
    [root@testvm02 ~]# cat nrpe.cfg 
    allowed_hosts=127.0.0.1,192.168.53.25
    allowed_hosts=127.0.0.1,192.168.53.25

    文档创建时间:2018年8月2日18:34:17

  • 相关阅读:
    02-Maven安装配置
    01-Maven功能特点
    安装Jenkins服务
    Python操作Saltstack
    基于Python自动上传包到nexus仓库
    避免代码merge后无法构建发布(GItlabCI + Jenkins)
    SonarQube-Centos环境设置为系统服务
    SonarQube配置LDAP认证集成
    Centos7 搭建Go语言编译环境
    OpenShift-EFK日志管理
  • 原文地址:https://www.cnblogs.com/chuanzhang053/p/9409006.html
Copyright © 2011-2022 走看看