Replace xml values -- Shell --


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Replace xml values -- Shell --
# 1  
Old 11-29-2008
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" encoding="UTF-8"?>
<Daten>
<export_date>2008-07-15 16:29:01</export_date>
<language>english</language>
===================================

temp.txt

===================================
All dates were exported at <export_date>
The text was translated in <language>
===================================

script.sh

===================================
#!/bin/bash

IFS=$'\n'

for i in `cat file.xml | sed 's/>/ /g' | awk '{print $1}' | sed 's/</ /g' | sed 's/ //g'`
do


for j in `grep "$i" temp.txt | sed 's/.*<//g'| sed 's/>.*//g' `
do
#echo $j
for h in `cat temp.txt | sed -e "s/"$j"/$(cat file.xml | grep "$j" | sed 's/>/ /g' | sed 's/</ /g' | awk '{print $2}')/g" >> temp2 `
do
echo $h
done
done


done
==================================================


The aim should be

All dates were exported at 2008-07-15 16:29:01
The text was translated in english.

Do you know how I could work on this?

thx
# 2  
Old 11-29-2008
Hi,

first save the desired information in two variables using process substitution:

Code:
read date lang < <(sed -n "s#.*date>\(.*\)</.*#\1#p;s#.*age>\(.*\)</lan.*#\1#p" file.xml)

Then substitute the markers in the temp file:

Code:
sed "s/<export_date>/${date}/;s/<language>/${lang}/" temp.txt

HTH Chris
# 3  
Old 11-29-2008
Sorry,

little typo. It must be:

Code:
{read date; read lang} < <(sed -n "s#.*date>\(.*\)</.*#\1#p;s#.*age>\(.*\)</lan.*#\1#p" test)

# 4  
Old 11-29-2008
And if that doesn't work (it did on zsh but not on bash):

Code:
set -- $(sed -n "s#.*date>\(.*\)</.*#\1#p;s#.*age>\(.*\)</lan.*#\1#p" file.xml )

sed "s/<export_date>/${1} ${2}/;s/<language>/${3}/" temp.txt

# 5  
Old 11-29-2008
Thanks a lot.
That has been helpful to me!

My last issue is that the original xml file included 420 lines.
Therefore if I create now all the variables manual and in the future will be change one line in the xml file or temp.txt, then I'm lost.
Have you any idee how can I use a simply way?

THX
# 6  
Old 11-29-2008
ah, i see, try:

Code:
while IFS="><" read a id val b 
do 
  sed -n "s#<${id}>#${val}#p" temp.txt
done < file.xml >> output

This will print the substitutions to a file called output.
# 7  
Old 11-29-2008
super recognition,

but if the temp.txt include some others line without variables "exp. just a text line" will be not forwarded to the output. Or some variables in xml file is empty will be also dropped.
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