用另一个文件的内容替换文件中的分隔文本块 [英] Replace delimited block of text in file with the contents of another file

查看:20
本文介绍了用另一个文件的内容替换文件中的分隔文本块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要编写一个简单的脚本,用另一个文件的内容替换配置文件中的一段文本.

I need to write a simple script to replace a block of text in a configuration file with the contents of another file.

假设有以下简化文件:

server.xml

<?xml version='1.0' encoding='UTF-8'?>
<Server port="8005" shutdown="SHUTDOWN">
  <Service name="Catalina">
    <Connector port="80" protocol="HTTP/1.1"/>
    <Engine name="Catalina" defaultHost="localhost">
      <!-- BEGIN realm -->
        <sometags/>
        <sometags/>
      <!-- END realm -->
      <Host name="localhost" appBase="webapps"/>
    </Engine>
  </Service>
</Server>

realm.xml

<Realm className="org.apache.catalina.realm.UserDatabaseRealm"
       resourceName="UserDatabase"/>

我想运行一个脚本并让 realm.xml 替换 <!-- BEGIN realm -->< 之间的内容.!-- END 领域 --> 行.如果 realm.xml 发生变化,那么每当脚本再次运行时,它将再次用 realm.xml 的新内容替换这些行.这旨在在域将不同的多个安装上启动服务时在 /etc/init.d/tomcat 中运行.

I want to run a script and have realm.xml replace the contents between the <!-- BEGIN realm --> and <!-- END realm --> lines. If realm.xml changes then whenever the script is run again it will replace the lines again with the new contents of realm.xml. This is intended to be run in /etc/init.d/tomcat on startup of the service on multiple installations on which the realm is going to be different.

我不太确定如何简单地使用 awksed 来做到这一点.

I'm not so sure how can I do this simply with awk or sed.

推荐答案

试试这个:

sed -i -ne '/<!-- BEGIN realm -->/ {p; r realm.xml' -e ':a; n; /<!-- END realm -->/ {p; b}; ba}; p' server.xml

这篇关于用另一个文件的内容替换文件中的分隔文本块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆