简体   繁体   English

linux shell:sed替换xml中的值

[英]linux shell: sed replace value in xml

I have a xml file, I want to repace the text value in the tag < jdbcurl > with another value, but there are two tags named with jdbcurl nested in different pool id. 我有一个xml文件,我想用另一个值替换标记<jdbcurl>中的文本值,但是有两个标记为jdbcurl的标记嵌套在不同的池ID中。

Can any one do me a favor to dig it with SED? 请问有人可以用SED对其进行挖掘吗? Thanks. 谢谢。

<?xml version="1.0" ?>
  <WEBServer fileName="webdb.xml" name="Configuration and Security File">
  <security>
  <pool id="DEFAULT" jndiName="jdbc/webdb">
     <dbschema></dbschema>
     <userID>DBUSER</userID>
     <password>passwd1</password>
     <jdbcdriver>oracle.jdbc.driver.OracleDriver</jdbcdriver>
     <jdbcurl>jdbc:oracle:thin:@db.server.com:1753/ORCSN</jdbcurl>
  </pool>
   <pool id="bi_id" jndiName="jdbc/bidb">
    <dbschema></dbschema>
    <userID>BIUSER</userID>
    <password>passwd2</password>
    <jdbcdriver>oracle.jdbc.driver.OracleDriver</jdbcdriver>
    <jdbcurl>jdbc:oracle:thin:@db.server.com:1753/ORCSN</jdbcurl>
  </pool>
 </security>
 </WEBServer>
sed -E '/bi_id/,/pool/  s/jdbc:[^<]*/you will replace/g' filename

this one will replace jdbc in pool with id='bi_id' 这将用id ='bi_id'替换池中的jdbc

sed -E '/DEFAULT/,/pool/  s/jdbc:[^<]*/you will replace/g' 

this is for DEFAULT pool's jdbcurl 这是用于DEFAULT池的jdbcurl

With xmlstarlet: 使用xmlstarlet:

xmlstarlet edit --update '//WEBServer/security/pool[@id="DEFAULT"]/jdbcurl' --value 'XYZ' file.xml

Output: 输出:

<?xml version="1.0"?>
<WEBServer fileName="webdb.xml" name="Configuration and Security File">
  <security>
    <pool id="DEFAULT" jndiName="jdbc/webdb">
      <dbschema/>
      <userID>DBUSER</userID>
      <password>passwd1</password>
      <jdbcdriver>oracle.jdbc.driver.OracleDriver</jdbcdriver>
      <jdbcurl>XYZ</jdbcurl>
    </pool>
    <pool id="bi_id" jndiName="jdbc/bidb">
      <dbschema/>
      <userID>BIUSER</userID>
      <password>passwd2</password>
      <jdbcdriver>oracle.jdbc.driver.OracleDriver</jdbcdriver>
      <jdbcurl>jdbc:oracle:thin:@db.server.com:1753/ORCSN</jdbcurl>
    </pool>
  </security>
</WEBServer>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM