Insert the contents of one file into another file before the line that matches a regular expression

对着背影说爱祢 提交于 2019-12-12 03:38:59

问题


In a DockerFile, I'm starting with a TomEE base image. I need to modify the "conf/tomee.xml" to add a DataSource resource. I've stored the DataSource definition in a separate file. I want to find the "" end tag and insert the DataSource definition before that line.

I found this SO posting, which seems to be what I need, but for some reason this just isn't replacing the contents of the original file. I tried piping the result to the file, but that results in a zero-length file (as if it's writing its output to stderr, which I can't believe).

Here is the original contents of "conf/tomee.xml":

<?xml version="1.0" encoding="UTF-8"?>
<tomee>
  <!-- see http://tomee.apache.org/containers-and-resources.html -->

  <!-- activate next line to be able to deploy applications in apps -->
  <!-- <Deployments dir="apps" /> -->
</tomee>

Here is the script I have so far:

    excerpt=$(<${TOMEE_HOME}/tomee.xml.excerpt)
awk -vexcerpt="${excerpt}" '/<\/tomee>/{print excerpt;print;next}1' ${TOMEE_HOME}/conf/tomee.xml

Here is the "tomee.xml.excerpt" file:

  # The SID may very well be sus2, but it could be something else.
  <Resource id="sus2" type="DataSource">
    JdbcDriver = oracle.jdbc.driver.OracleDriver
    MaxActive = 10
    MinIdle = 2
    MaxIdle = 2 
    MaxWait = 10000
       JdbcUrl = jdbc:oracle:thin:@${DB_HOST}:${DB_PORT}:${DB_SID}
       UserName = ${DB_USER}
       Password = ${DB_PASSWORD}
  </Resource>

Note that this file has env var references. That obviously won't work as written, but I will address that when I figure out how to get the excerpt file into the correct place in the tomee.xml file.

The following is what I see when I run the script manually:

+ excerpt='  # The SID may very well be sus2, but it could be something else.
  <Resource id="sus2" type="DataSource">
    JdbcDriver = oracle.jdbc.driver.OracleDriver
    MaxActive = 10
    MinIdle = 2
    MaxIdle = 2 
    MaxWait = 10000
       JdbcUrl = jdbc:oracle:thin:@${DB_HOST}:${DB_PORT}:${DB_SID}
       UserName = ${DB_USER}
       Password = ${DB_PASSWORD}
  </Resource>'
+ awk '-vexcerpt=  # The SID may very well be sus2, but it could be something else.
  <Resource id="sus2" type="DataSource">
    JdbcDriver = oracle.jdbc.driver.OracleDriver
    MaxActive = 10
    MinIdle = 2
    MaxIdle = 2 
    MaxWait = 10000
       JdbcUrl = jdbc:oracle:thin:@${DB_HOST}:${DB_PORT}:${DB_SID}
       UserName = ${DB_USER}
       Password = ${DB_PASSWORD}
  </Resource>' '/<\/tomee>/{print excerpt;print;next}1' /usr/local/tomee/conf/tomee.xml
<?xml version="1.0" encoding="UTF-8"?>
<tomee>
  <!-- see http://tomee.apache.org/containers-and-resources.html -->

  <!-- activate next line to be able to deploy applications in apps -->
  <!-- <Deployments dir="apps" /> -->
  # The SID may very well be sus2, but it could be something else.
  <Resource id="sus2" type="DataSource">
    JdbcDriver = oracle.jdbc.driver.OracleDriver
    MaxActive = 10
    MinIdle = 2
    MaxIdle = 2 
    MaxWait = 10000
       JdbcUrl = jdbc:oracle:thin:@${DB_HOST}:${DB_PORT}:${DB_SID}
       UserName = ${DB_USER}
       Password = ${DB_PASSWORD}
  </Resource>
</tomee>

When I inspect the "conf/tomee.xml" file, its contents haven't changed, including the last modification timestamp.


回答1:


All of the answers on the page you reference are wrong. Do this to print the contents of excerpt file exactly as-is:

awk 'NR==FNR{excerpt=$0;next} {print} /<\/tomee>/{print excerpt}' RS='^$' "${TOMEE_HOME}/tomee.xml.excerpt" RS="\n" "${TOMEE_HOME}/conf/tomee.xml" > tmp && mv tmp "${TOMEE_HOME}/conf/tomee.xml"

The above uses GNU awk for multi-char RS but you were already gawk-specific due to no space after -v.




回答2:


Side note on that topic: tomee.xml is one way to configure tomee but not very nice to dynamically configure the server, an alternative is to use conf/system.properties syntax this way you add the resource you want at the end without caring of XML:

db = new://Resource?type=DataSource
db.JdbcUrl = jdbc:oracle:thin:@xxx:yyyy:zzzz
db.UserName = aaaaa
....


来源:https://stackoverflow.com/questions/38966026/insert-the-contents-of-one-file-into-another-file-before-the-line-that-matches-a

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!