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

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

问题描述

我需要写一个简单的脚本来替换文本块与另一个文件的内容配置文件。

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

让我们有以下简化文件假设:

Let's assume with have the following simplified files:

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 更换之间的&LT的内容;! - BEGIN境界 - &GT; &LT;! - END境界 - &GT; 线。如果 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.

我不太肯定我怎么可以这样简单地用 AWK SED

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天全站免登陆