Need help to change XML values with shell scripting for Network Simulation

Hello,
I don't have experience in this scripting and I need some help to read a value from an XML file and change it with a random number to use in simulator for different network scenarios.

</Description><sim_comm_rounds>35</sim_comm_rounds><num_clusters>1</num_clusters><Clocking>

I want to change the "num_clusters" value with a random number ( at first, a constant is also fine ) the file name is: 1_cluster_TTP.dat.xml I already figured to read the value. The code is like this:

awk '/<num_clusters>/,/<\/num_clusters>/' 1_cluster_TTP.dat.xml | sed 's/\(.*\)\(<num_clusters>\)\(.*\)\(<\/num_clusters>\)\(.*\)/\3/'

I want to change this value for instance to "2". Any help would be appreciated. Thank you

Try:

value=2
sed "s!\(<num_clusters>\)[0-9]*\(</num_clusters>\)!\1${value}\2!" 1_cluster_TTP.dat.xml

Jean-Pierre.

1 Like

Hi thanks for your help but I get this error message:
bash: !\: event not found

It's because you execute it on the command line. In a script this code should work. But you can change "!" to something else - "|" or "#" or "^" etc. and everything should work.

Can you tell me which "!" to change because I couldn't do it..sorry cheers

---------- Post updated at 12:08 PM ---------- Previous update was at 11:56 AM ----------

Okay I think I got it..but it doesn't change the value in the file, it just shows the XML code with new value in the command prompt screen. What should I do to save the new value in the file?

With GNU sed you can use option "-i.bak" to write the result to 1_cluster_TTP.dat.xml. You will have the old content in 1_cluster_TTP.dat.xml.bak file.

With others you can do "sed ... 1_cluster_TTP.dat.xml > 1_cluster_TTP.dat.xml.new", check the new file and if everything is OK, mv it to 1_cluster_TTP.dat.xml.