简体   繁体   中英

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.

Can any one do me a favor to dig it with 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'

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

this is for DEFAULT pool's jdbcurl

With 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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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