2

I have an XML file like below. I want change the values at is-required place and default-value place of each argument name using a shell script..

  • where argument name=protocol then is-required = "true" and default-value=tcp,
  • where argument name =port then is-required = "true" default-value= 7223,
  • where argument name =then and is-required = "true" default-value=test,

Example:

<task-arguments>
    <argument name="protocol" is-required="false" default-value="ssl"/>
    <argument name="port" is-required="true" default-value="7222"/>
    <argument name="username" is-required="true" default-value="admin"/>
</task-arguments>
Satō Katsura
  • 13,368
  • 2
  • 31
  • 50

1 Answers1

7

Using XMLStarlet:

xml ed -P \
    -u '//argument[@name="protocol"]/@is-required' -v true \
    -u '//argument[@name="protocol"]/@default-value' -v tcp \
    -u '//argument[@name="port"]/@is-required' -v true \
    -u '//argument[@name="port"]/@default-value' -v 7223 \
    -u '//argument[@name="username"]/@is-required' -v true \
    -u '//argument[@name="username"]/@default-value' -v admin \
        file.xml

Result for your sample:

<task-arguments>
    <argument name="protocol" is-required="true" default-value="tcp"/>
    <argument name="port" is-required="true" default-value="7223"/>
    <argument name="username" is-required="true" default-value="admin"/>
</task-arguments>
Satō Katsura
  • 13,368
  • 2
  • 31
  • 50