Quantcast
Channel: Inserting a text between 2 patterns - Unix & Linux Stack Exchange
Viewing all articles
Browse latest Browse all 4

Answer by chaos for Inserting a text between 2 patterns

$
0
0

The file that is given is not valid xml. You have to add opening and closing root tags. Like this:

<xml>
  <controller-host></controller-host>
  <controller-port></controller-port>
  <tier-name></tier-name>
  <node-name></node-name>
</xml>

With that input you can use an xml parser like xmlstartlet to edit the contents:

xmlstarlet ed -O -u //controller-host -v "AppDynamicsHost" \
                 -u //controller-port -v "80" \
                 -u //tier-name -v "the hostname" \
                 -u //node-name -v "tomcat server" file
  • ed start editing mode of xmlstartlet
  • -O omit the <?xml version="1.0"?> declaration
  • -u update the given xpath
  • -v set the given value

The output would then be:

<xml>
  <controller-host>AppDynamicsHost</controller-host>
  <controller-port>80</controller-port>
  <tier-name>the hostname</tier-name>
  <node-name>tomcat server</node-name>
</xml>

To edit the file directly inplace you can use the -L flag.


Viewing all articles
Browse latest Browse all 4

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>