I have xml file like below, i want change the values at default-value place of each argument name using shell script. like
where argument name= protocol and default-value=tcp,
where argument name =port and default-value= 7223,
where argument name = username and default-value=test,
Do you just want to change <argument name> tag data if the tag appears between <task-arguments> and </task-arguments> tags; or do you want to change them everywhere they appear?
Where do the new values come from? Is there a configuration file? Are they given as command-line arguments? Are they to be hardcoded in your script?
Are only the values shown in the example to be replaced, or are all values found in your input file to be replaced?
What is the name of your XML file? Is the name to be given as a parameter to your script? Is the name to be hard coded into your script?
What have you tried to solve this problem on your own?
Hi Don,
Please see my comment below..
What operating system and shell are you using?
RHEL
Do you just want to change <argument name> tag data if the tag appears between <task-arguments> and </task-arguments> tags; or do you want to change them everywhere they appear?
I want to change the values only the tag b/w <task-arguments> and </task-arguments> tags.
Where do the new values come from? Is there a configuration file? Are they given as command-line arguments? Are they to be hardcoded in your script?
Those values are coming from configuration file..
Are only the values shown in the example to be replaced, or are all values found in your input file to be replaced?
All the values to be replaced which are b/w the tag.
What is the name of your XML file? Is the name to be given as a parameter to your script? Is the name to be hard coded into your script?
monitor.xml and the value is hard coded
What have you tried to solve this problem on your own?
tried like below
sed -i '/<argument\s\+name="hostname"/s/is-required="*.*"/is-required="$REQ1" default-value="$hostname"/' $file
I find it difficult to believe that above sed script worked the desired way. Within single quotes, shell variables are not expanded, so the literal values like ${REQ1} would be inserted into the output:
I gave only example in earlier post, not full of my script. I ma trying understand the logic to implement, I Will declare the values in script,how to pars the values my script will take care.
At the end i am looking a command to update the values at XML file...
---------- Post updated at 06:02 PM ---------- Previous update was at 04:03 PM ----------
Hi Rudi,
You are correct, my script is inserting ${REQ1} values only.. help me the right command to use.
I have used XMLSTARTLET package and used the bellow command, still no luck,
xml ed -u '/task-arguments/argument[@name="protocol"]/@default-value' -v 'string("tcp")' /tmp/test_xml/monitor.xml
xml ed -u '/task-arguments/argument[@name="port"]/@default-value' -v 'string("7223")' /tmp/test_xml/monitor.xml
xml ed -u '/task-arguments/argument[@name="username"]/@default-value' -v 'string("test")' /tmp/test_xml/monitor.xml
i am getting bellow error,
./sed.sh: line 4: 23932 Segmentation fault xml ed -u '/task-arguments/argument[@name="protocol"]/@default-value' -v 'string("tcp")' /tmp/test_xml/monitor.xml
./sed.sh: line 5: 23933 Segmentation fault xml ed -u '/task-arguments/argument[@name="port"]/@default-value' -v 'string("7223")' /tmp/test_xml/monitor.xml
./sed.sh: line 6: 23934 Segmentation fault xml ed -u '/task-arguments/argument[@name="username"]/@default-value' -v 'string("test")' /tmp/test_xml/monitor.xml
please help me on this..
---------- Post updated at 06:07 PM ---------- Previous update was at 02:20 PM ----------
Hi Rudi,
The below one help me achieve my requirement..Thanks for your time
#!/bin/sh
xmlfile_in="data.xml"
xmlfile_out="data.new"
protocol="tcp"
port="7223"
user="test"
sed -f /dev/stdin "$xmlfile_in" >"$xmlfile_out" <<END_SED
/argument name="protocol"/{
i\\
<argument name="protocol" is-required="true" default-value="$protocol"/>
d
}
/argument name="port"/{
i\\
<argument name="port" is-required="true" default-value="$port"/>
d
}
/argument name="username"/{
i\\
<argument name="username" is-required="true" default-value="$user"/>
d
}
END_SED