XML- Sed || Awk Bash script... Help!


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting XML- Sed || Awk Bash script... Help!
# 1  
Old 08-26-2011
Question XML- Sed || Awk Bash script... Help!

Hi ! I'm working into my first bash script to make some xml modification and it's going to make me crazy lol .. so I decide to try into this forum to take some ideas from people that really know about this!

This is my situation I've and xml file with a lots of positional values with another tags too but this are the importants tags
Code:
<x>28.981743194648736</x>
<y>72.4853691731618</y>
<z>0.0</z>
<x>53.46605522341658</x>
<y>54.678596788603365</y>
<z>0.0</z>

and this continue with differents values in the same way
I need to change all the values of x tags by new ones, particulary with random numbers.. the problem that I've is that I have succes to change that values but..... ! , sed change me the value of all the <x> </x> tags with the same value and I need that all that values were differents.. here my code
Code:
#!/bin/bash
NUM=0

while [ $NUM -le 20 ]; do
NUM1=$(echo "scale=3;((${RANDOM})/1000)" |bc)
echo "$NUM1"
sed 's/<x>[-[:alnum:]./]\{1,\}<\/x>/<x>'$NUM1'<\/x>/' myfile.xml > newfile.xml

echo "\$NUM: $NUM"
let NUM=$NUM+1
done

THANKS FOR YOUR TIME ANY IDEA WOULD BE WELCOME !!!

Last edited by Franklin52; 08-28-2011 at 08:56 AM.. Reason: Please use code tags for data and code samples, thank you
# 2  
Old 08-26-2011
line-basedtools like awk, sed, shells, aren't very good at handling xml because they're not recursive parsers. (Well, it parses THEIR language recursively, but doesn't parse text they read in recursively -- things like yacc do..) Usually you'd use a language like perl or python and load an xml module. Could you tell us what your actual goal is instead of the way you want to do it?

Last edited by Corona688; 08-26-2011 at 02:41 AM..
# 3  
Old 08-26-2011
note that everytime your run this script, you get the same x value in same line. but in the file, every x-value should be unique. If you want to get different x-value in each run, change the srand seed please.

Code:
awk '/x/{srand(NR);v=rand()*50+1;$0="<x>"v"</x>"};1' yourFile

output:
Code:
<x>12.8894</x>
<y>72.4853691731618</y>
<z>0.0</z>
<x>37.5135</x>
<y>54.678596788603365</y>
<z>0.0</z>

This User Gave Thanks to sk1418 For This Post:
# 4  
Old 08-26-2011
Thanks Corona668 and sk1418 for your time ! . .

sk1418 your code works like a charm! thanks for your help !, another question...... sorry if I bother you, I almost know nothing of awk , you can put the "output" in a new file like in sed ? and another thing ! you can define how many decimals you need ? like 3.444 or 3.44444444
Code:
sed 's/<x>[-[:alnum:]./]\{1,\}<\/x>/<x>'$NUM1'<\/x>/' cornodobeta2.xml > newfile.xml

I tried with > newfile.xml but no lucky

Thanks for your help !!

Last edited by Franklin52; 08-28-2011 at 08:57 AM.. Reason: Please use code tags for data and code samples, thank you
# 5  
Old 08-26-2011
Quote:
Originally Posted by juampal
Thanks Corona668 and sk1418 for your time ! . .

sk1418 your code works like a charm! thanks for your help !, another question...... sorry if I bother you, I almost know nothing of awk , you can put the "output" in a new file like in sed ? and another thing ! you can define how many decimals you need ? like 3.444 or 3.44444444

sed 's/<x>[-[:alnum:]./]\{1,\}<\/x>/<x>'$NUM1'<\/x>/' cornodobeta2.xml > newfile.xml

I tried with > newfile.xml but no lucky

Thanks for your help !!
For your Q1: write the output to a new file is as easy as:
Code:
awk ... inputFile > yourNewFile

Q2: with precision control:
Code:
awk '/x/{srand(NR);v=rand()*50+1;printf  "<x>%.13f</x>\n",v};!/x/'

you can change the "%.13f" to your wish to control the precision. the output of above line with %.13f looks like:
Code:
<x>12.8893756120882</x>
<y>72.4853691731618</y>
<z>0.0</z>
<x>37.5135307593800</x>
<y>54.678596788603365</y>
<z>0.0</z>

This User Gave Thanks to sk1418 For This Post:
# 6  
Old 08-26-2011
Man ... you're simply awesome!
Supose that in the future I need to change the <y> value too, with a value distinct to the x and also random the code will be this ?
Code:
awk '/x/{srand(NR);v=rand()*50+1;$0="<x>"v"</x>"};1' myfile.xml > filemod.xml
awk '/y/{srand(NR);v=rand()*50+1;$0="<y>"v"</y>"};1' myfile.xml > filemod.xml

because it doesn't give me the expected result .

Here I Post part of the exactly xml that I'm working .

Code:
<mote> 
      <interface_config> 
        se.sics.cooja.interfaces.Position 
        <x>53.39887462699737</x> 
        <y>66.79001043503861</y> 
        <z>0.0</z> 
      </interface_config> 
      <interface_config> 
        se.sics.cooja.contikimote.interfaces.ContikiMoteID 
        <id>4</id>

Thanks Again for your Time and your experience in this !

Last edited by Franklin52; 08-28-2011 at 08:57 AM.. Reason: Please use code tags for data and code samples, thank you
# 7  
Old 08-26-2011
you can change the random seed to get different random number for your 'y'.
eg
Code:
awk '/y/{srand(NR*2);v=rand()*77+1;$0="<y>"v"</y>"};1' myfile.xml > filemod.xml

I think it would be ok. (I didn't test).

In fact the easiest way to get a new y is do some math calculation on generated x. e.g. sqr(x) or x + a magic number ...
This User Gave Thanks to sk1418 For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Text manipulation with sed/awk in a bash script

Guys, I have a variable in a script that I want to transform to into something else Im hoping you guys can help. It doesn't have to use sed/awk but I figured these would be the simplest. DATE=20160120 I'd like to transform $DATE into "01-20-16" and move it into a new variable called... (8 Replies)
Discussion started by: dendenyc
8 Replies

2. Shell Programming and Scripting

Modify xml using sed or awk

Hi All, I want to modify(changing the status from "on" to "off" status of Stage-element value from the below xml file using sed or awk: File Name: global.xml <?xml version="1.0" encoding="UTF-8"?> <config> <widget> <name>HTTP-POOL</name> <attributes> ... (5 Replies)
Discussion started by: wamqemail2
5 Replies

3. Shell Programming and Scripting

Rsync script to rewrite suffix - BASH, awk, sed, perl?

trying to write up a script to put the suffix back. heres what I have but can't get it to do anything :( would like it to be name.date.suffix rsync -zrlpoDtub --suffix=".`date +%Y%m%d%k%M%S`.~" --bwlimit=1024 /mymounts/test1/ /mymounts/test2/ while IFS=. read -r -u 9 -d '' name... (1 Reply)
Discussion started by: jmituzas
1 Replies

4. Shell Programming and Scripting

'Couldn't read file' error in bash script with expect, sed and awk!

Ok, so I have a bash script with an embedded expect statement. Inside of the expect statement, i'm trying to pull all of the non-comment lines from the /etc/oratab file one at a time. Here's my command: cat /etc/oratab |sed /^s*#/d\ | awk 'NR==1'|awk -F: '{print \"$1\"}'|. oraenv Now,... (0 Replies)
Discussion started by: alexdglover
0 Replies

5. UNIX for Dummies Questions & Answers

xml to csv using sed and awk command

Hi Guys, Can you help me in creating shell script using sed,awk etc commands to generate csv file using xml file. (5 Replies)
Discussion started by: sbk
5 Replies

6. Shell Programming and Scripting

Need help in using sed/awk for line insertion in xml

Hello, I have two text files (txt1 and txt2). txt1 contains many lines with a single number in each line. txt2 (xml format) contains information about the numbers given in txt1. I need to insert one line in txt2 within the scope of each number taken from txt1. Sample problem: txt1: 12 23... (1 Reply)
Discussion started by: shekhar2010us
1 Replies

7. Shell Programming and Scripting

Using SED/AWK to extract xml at end of file

Hello everyone, Firstly i do not require alot of help.. i am right at the end of finishing my scipt but cannot find a solution to the last part. What i need to do is, prompt the user for a file to work with, which i have done. promt the user for an output file - which is done. #!/bin/bash... (14 Replies)
Discussion started by: hugh86
14 Replies

8. Shell Programming and Scripting

Need to Parse XML from bash script

I am completely new to bash scripting and now need to write a bash script that would parse a XML file and take out values from specific tags. I tried using xsltproc, xml_grep commands. But the issue is that the XML i am trying to parse is not UTF 8. so those commands are unable to parse my XML's... (4 Replies)
Discussion started by: shivashankar.g
4 Replies

9. Shell Programming and Scripting

sed or awk to extract data from Xml file

Hi, I want to get data from Xml file by using sed or awk command. I want to get the following result : mon titre 1;Createur1;Dossier1 mon titre 1;Createur1;Dossier1 and save it in cvs file (fichier.cvs). FROM this Xml file (test.xml): <playlist version="1"> <trackList> <track>... (1 Reply)
Discussion started by: yeclota
1 Replies

10. Shell Programming and Scripting

parsing xml with awk/sed

Hi people!, I need extract from the file (test-file.txt) the values between <context> and </context> tag's , the total are 7 lines,but i can only get 5 or 2 lines!!:confused: Please look my code: #awk '/context/{flag=1} /\/context/{flag=0} !/context/{ if (flag==1) p rint $0; }'... (3 Replies)
Discussion started by: ricgamch
3 Replies
Login or Register to Ask a Question