AIX UNIX Script to Replace XML Values


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting AIX UNIX Script to Replace XML Values
# 1  
Old 12-22-2015
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 an XML when a match is found.
For example:
Flat file contains multiple entries in the format of -

Code:
Application.Env~DEV 
Application.ID~99999

Pre-script XML contains multiple entries in the format of -

Code:
<name>Application/Env</name> 
<value>XXX</value> 
<name>Application/ID</name> 
<value>00000</value>

How do I replace <value> tags in the XML with the text after '~' when the <name> tag matches the part before '~'; Essentially, parsing the flat file based on the tildes. So after running the script I will have

Post-script XML:
Code:
<name>Application/Env</name> 
<value>DEV</value> 
<name>Application/ID</name> 
<value>99999</value>

Any suggestions are appreciated. I don't have code to share, I just need a starting point.

Moderator's Comments:
Mod Comment edit by bakunin: please use CODE-tags as you agreed to do when you accepted the forum rules. Thank you.

Last edited by bakunin; 12-22-2015 at 11:44 AM..
# 2  
Old 12-22-2015
Please use code tags as required by forum rules!

Not sure I understand. Text and samples seem somewhat contradictory. Does your flat file contain one or several replacement entries? Which one to use, then? Do you want to replace every occurrence in the XML file with the replacement values? If no, which to replace?
# 3  
Old 12-22-2015
Code tags noted.

Text file contains several entries in the format described and yes the XML will be replaced with every occurrence in the text file.
# 4  
Old 12-22-2015
Quote:
Originally Posted by ocbit
.
.
.the XML will be replaced with every occurrence in the text file.
How that?

For your limited sample, try
Code:
awk '
FNR == NR       {sub (/\./, "/", $1)
                 T[$1] = $2
                 next
                }
                {for (t in T) if ($0 ~ t) TF = t
                }
TF && /<value/  {sub (/>[^<]*</, ">" T[TF] "<")
                 TF = ""
                }
1
' FS="~" flatfile xmlfile
<name>Application/Env</name>
<value>DEV</value>
<name>Application/ID</name>
<value>99999</value>

This User Gave Thanks to RudiC For This Post:
# 5  
Old 12-22-2015
Thank you RudiC, the replacements are working perfectly.

Last edited by ocbit; 12-22-2015 at 05:00 PM.. Reason: No need.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Extract values from xml file script

Hi, please help on this. I want extract values of xml file structure and print in determined way. <ProjectName> --> only appears once <StructList> --> is the top node <Struct> node --> could be more than 1 NameID, STX, STY, PRX, PRY --> appears only 1 time within each <Struct> node... (10 Replies)
Discussion started by: Ophiuchus
10 Replies

2. UNIX for Beginners Questions & Answers

Script to populate (2) values in .XML

Good Afternoon Team - I"m asking for assistance on a piece of code to populate two values in an XML file. I have it working perfectly using CScript for DOS, but I have a need to do that same process in a Linux environment. Here is the XML I need to modify: <?xml version="1.0"... (5 Replies)
Discussion started by: SIMMS7400
5 Replies

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

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

5. Shell Programming and Scripting

Help needed :Search and Replace a string pattern with empty in an xml file in unix

Search and Replace a string pattern with empty in an xml file in unix: My xml file would be like this : <Accounts><Name>Harish</Name><mobile>90844444444444445999 </mobile><TRIG>srcujim-1</TRIG></Accounts><Accounts><Name>Satish</Name><mobile>908999</mobile><TRIG>ettertrtt-1</TRIG></Accounts> ... (1 Reply)
Discussion started by: harish_s_ampeo
1 Replies

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

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

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

9. Shell Programming and Scripting

KSH Script to Get the <TAG Values> from an XML file

Hi All, I am new to Unix I need a KSH script to get the values from XML file to write to a temp file. Like the requirement is from the below TAG <MAPPING DESCRIPTION ="Test Mapping" ISVALID ="YES" NAME ="m_test_xml" OBJECTVERSION ="1" VERSIONNUMBER ="1"> I need the MAPPING DESCRIPTION... (3 Replies)
Discussion started by: perlamohan
3 Replies
Login or Register to Ask a Question