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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Need help to change XML values with shell scripting for Network Simulation
# 1  
Old 08-02-2011
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.
Code:
</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:
Code:
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

Last edited by radoulov; 08-02-2011 at 11:14 AM.. Reason: Code tags.
# 2  
Old 08-02-2011
Try:
Code:
value=2
sed "s!\(<num_clusters>\)[0-9]*\(</num_clusters>\)!\1${value}\2!" 1_cluster_TTP.dat.xml

Jean-Pierre.
This User Gave Thanks to aigles For This Post:
# 3  
Old 08-02-2011
Hi thanks for your help but I get this error message:
bash: !\: event not found
# 4  
Old 08-02-2011
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.
# 5  
Old 08-02-2011
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?
# 6  
Old 08-02-2011
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.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to change values in xml file?

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, example ... (12 Replies)
Discussion started by: s1s2s3s4
12 Replies

2. Shell Programming and Scripting

Passing values to an XML file from shell script

:wall: Hi, I have an XML file with 5 tags. I need to pass values to the XML file from a shell script that will replace values in 2 of the tags. I cannot hardcode the tag values in XML and use replace command in script as the values are likely to change. Please help !!!!!!!!!!! (2 Replies)
Discussion started by: Monalisaa
2 Replies

3. Shell Programming and Scripting

Change attribute value in xml using shell script

hi, i am new to unix and i have a problem. -------------------------------------------------------------- sebben.xml <envelope> <email> sebben@example.com </email> </envelope> script_mail written in the vi editor. #!/bin/sh script to change the value in attribute <email> echo... (3 Replies)
Discussion started by: sebbenw
3 Replies

4. Shell Programming and Scripting

XML-Text Parsing Using shell scripting

HI Guys, I have to parse below xml file :- <xn:SubNetwork id="ONRM_ROOT_MO_R"> <xn:MeContext id="LP101"> <xn:ManagedElement id="1"> <xn:VsDataContainer id="1"> <xn:attributes> ... (8 Replies)
Discussion started by: asavaliya
8 Replies

5. Solaris

XML to Text file Parsing Using shell scripting

Hi, I want to parse an XML File using Shell Script preferably by using awk command, I/P file is : <gn:ExternalGsmCell id="016P3A"> <gn:attributes> <gn:mnc>410</gn:mnc> <gn:mcc>310</gn:mcc> <gn:lac>8016</gn:lac> ... (2 Replies)
Discussion started by: tech_frk
2 Replies

6. Shell Programming and Scripting

XML to Text file Parsing Using shell scripting

Hi folks, Need some help with XML to text file parsing , the following is the content of the XML File. <xn:SubNetwork id="SNJNPRZDCR0R03"> <xn:MeContext id="PRSJU0005"> <xn:VsDataContainer id="PRSJU0005"> <xn:attributes> ... (6 Replies)
Discussion started by: tech_frk
6 Replies

7. Shell Programming and Scripting

Need help to change values in XML using Python? noob help pls!

Hello I am a noob in XML and Python. I am trying to do this for my MSc project about a network simulation and need some help.... I want to change the values shown below: <num_crash_failures>1</num_crash_failures> −<crash_failure_entry> <freeze_at_slot>0</freeze_at_slot>... (0 Replies)
Discussion started by: erhanasd
0 Replies

8. Shell Programming and Scripting

Change values in Log4j.xml using ksh script

Hi, I am new to UNIX and shell scripting. I have to create a shell script(ksh) which parses log4j.xml file for a given webservice name and change the corresponding value from INFO to DEBUG or vice-versa. My log4j.xml looks like:- <!-- Appender WEBSERVICENAME--> <appender... (3 Replies)
Discussion started by: sanjeevcseng
3 Replies

9. Shell Programming and Scripting

using XML::simple in shell scripting

CPAN provide xml::simple module which can be used to generate XML files. can i use this module in shell scripting? how? plzz give me an example to generate xml file using this module . you can also use some other module.. BUT SHELL SCRIPTING SHOULD ONLY BE USED (4 Replies)
Discussion started by: cynosure2009
4 Replies

10. Shell Programming and Scripting

Replace xml values -- Shell --

Hello all, I try to create a bash script but till now without any positiv results. The script should replace different variables in a text file with the right xml values Look at the following xml file: file.xml =================================== <?xml version="1.0"... (14 Replies)
Discussion started by: research3
14 Replies
Login or Register to Ask a Question