Replace xml values -- Shell --


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Replace xml values -- Shell --
# 8  
Old 11-29-2008
I don't understand what you mean by your second point "Or some variables
in xml file is empty will be also dropped." The first problem should be solved.
But if you have two files with arbitrary data in them, you will have to use
more powerful tools like awk or perl and provide some more lines of examples of input and desired output.

Code:
while IFS=">" read a id val 
do 
    if [[ "$id" =~ "</" ]] then 
        VAL=${id%<*}; ID=${id#*</}
        sed "s/<${ID}>/${VAL}/" temp.txt >> temp
        mv temp temp.txt
    fi
done < file.xml

# 9  
Old 11-29-2008
With the second point I mean if not values are setting exp. <test></test>.
You are right, with perl or awk is more easy to finish this task but unfortunately I don't have any experience in awk or perl.

Whatever the stdout now is:

Syntax error: "fi" unexpected (expecting "then")
# 10  
Old 11-30-2008
Try to think a little bit. There will be no stdout as the output is
redirected to file called temp and under bash you have to put
the "then" on a new line.

Code:
while IFS=">" read a id val 
do 
    if [[ "$id" =~ "</" ]] 
    then 
        VAL=${id%<*}; ID=${id#*</}
        sed "s/<${ID}>/${VAL}/" temp.txt >> temp
        mv temp temp.txt
    fi
done < file.xml

# 11  
Old 11-30-2008
„Try to think a little bit. „ it's difficult to think at 3'o clock in the morning.
The script works, there is only one mistake left. If the variable of the xml file contains a slash, for example <Name>I/you/he/she/</Name> than the sed doesn't work.

My suggestion would be:

VAL1=$(echo "$id" | sed 's/\//\\\//g' | sed 's/</ </g' | awk '{print $1}')

sed "s/<${ID}>/${VAL1}/" temp.txt >> temp




Unfortunately other xml records will be ignored after the first column.
Exp. <Firstname>Jan Janus</Firstname>
In this example only the record Jan is recognized.
# 12  
Old 11-30-2008
This is the new xml-file with your previously unmentioned special cases:

Code:
<?xml version="1.0" encoding="UTF-8"?>
<Daten>
<export_date>2008-07-15 16:29:01</export_date>
<language>english</language>
<Name>I/you/he/she/</Name> 
<Firstname>Jan Janus</Firstname>

This is the code:

Code:
while IFS=">" read a id val 
do 
    if [[ "$id" =~ "</" ]] 
    then 
        VAL=${id%<*}; ID=${id#*</}
        sed "s|<${ID}>|${VAL}|" temp.txt >> temp
        mv temp temp.txt
    fi
done < file.xml

This is my output:

Code:
All dates were exported at 2008-07-15 16:29:01
The text was translated in english
And his name is I/you/he/she/
And his other name ist Jan

Notice, everything you had to change was replace the
"/" with "|" in the above sed statement.

For me the above code works flawlessly under zsh and bash.
# 13  
Old 11-30-2008
Or with escaped slashes:

Code:
#!/bin/bash

while IFS=">" read a id val 
do 
    if [[ "$id" =~ "</" ]] 
    then 
        VAL=${id%<*}; ID=${id#*</}
        VAL=${VAL//\//\\\/};ID=${ID//\//\\\/};
        sed "s/<${ID}>/${VAL}/" ~/temp.txt >> ~/temp
        mv ~/temp ~/temp.txt
    fi
done < ~/file.xml

In the above output example the Janus after Jan is missing
(the script works but not my mouse).

But if there are still more special cases i would throw the script
away and start from scratch with perl or awk.
# 14  
Old 11-30-2008
Christoph, thank you so much for your help.
I really appreciate it.

thx
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Getting error while including values in xml tags using shell script

Hi All, Please find the code below where I want to add the variable value in between the XML tags. I am taking one string and my goal is to put them between the xml tags. Ex : in between <name> , <lname> Kindly suggest a correction because while executing this script I am getting and... (8 Replies)
Discussion started by: rajneesh4U
8 Replies

2. Shell Programming and Scripting

AIX UNIX Script to Replace XML Values

Hi - I've seen variations of this same question asked but I have not been able to find an answer that fits my problem. Please direct me to another post if there already is a solution to this. I'm trying to write a Unix script to dynamically iterate through a flat file and replace a value in... (4 Replies)
Discussion started by: ocbit
4 Replies

3. Red Hat

How to replace Ip address in .xml file through shell script?

I have one .xml file. which contains the following line. <ParamString StringId="PortAddress" StringValue="172.27.166.170" /> <ParamString StringId="PortAddress" StringValue="172.27.166.171" /> <ParamString StringId="PortAddress" StringValue="172.27.166.202" /> <ParamString... (9 Replies)
Discussion started by: Anjan Ganguly
9 Replies

4. UNIX for Dummies Questions & Answers

Reading XML file and print the values in the text file using Linux shell script

hi guys, i want help... Reding XML file and print the values into the text file using linux shell script file as per below xml file <sequence> <Filename>aldorzum.doc</Filename> <DivisionCode>US</DivisionCode> <ContentType>Template</ContentType> <ProductCode>VIMZIM</ProductCode> </sequence>... (1 Reply)
Discussion started by: sravanreddy
1 Replies

5. 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

6. Shell Programming and Scripting

Shell Command to compare two xml lines while ignoring xml tags

I've got two different files and want to compare them. File 1 : HTML Code: <response ticketId="944" type="getQueryResults"><status>COMPLETE</status><description>Query results fetched successfully</description><recordSet totalCount="1" type="sms_records"><record... (1 Reply)
Discussion started by: Shaishav Shah
1 Replies

7. Shell Programming and Scripting

Shell or perl script to replace XML text in bulk

Hi, I am looking for assistance over shell or perl (without XML twig module) which replace string in XML file under particular branch..example of code file sample.. Exact requirment : Replace "Su saldo es" in below file with "Your balance" but only in XML branch of Text id=98 and Text Id=12... (7 Replies)
Discussion started by: Ashu_099
7 Replies

8. Shell Programming and Scripting

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... (5 Replies)
Discussion started by: erhanasd
5 Replies

9. Web Development

Replace xml values

Hallo 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"... (1 Reply)
Discussion started by: research3
1 Replies
Login or Register to Ask a Question