Overriding XML File


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Overriding XML File
# 1  
Old 04-12-2011
Overriding XML File

Hi All,

I have an XML file (normally >3mb). I need to loop through this and override with some new (correct) values.

Here is a row of data:-
<row>
<field name="ID">1</field>
<field name="Type">a</field>
<field name="value1">xxx</field>
<field name="value2">xxxx</field>
....
</row>

I need to loop through every instance of "row" and depending on the type value update the fields below it (inside that row element).

Does anyone have any ideas on how i could update everything in the selected <row> element?

Something like:-

for each row
if type is "a"
row[x] value1 = $newvalue
# 2  
Old 04-12-2011
You could do this in sed, by pulling each whole row into the buffer and then substituting:
Code:
sed '
  /<row>/{
    :loop
    /<\/row>/!{
      N
      b loop
     }
    s/\(<field name="Type">a<.*<field name="value1">\)[^<]*</\1'"$newvalue"'</
    t
    s/\(<field name="Type">b<.*<field name="value1">\)[^<]*</\1'"$newvalue2"'</
   }
 ' infile >outfile

You can add in more substitutes s/pattern/new/ and t for each case; the t between substitutes spits the row out if there was a substitute, so it is not scanned for substitutions twice.

This is also pretty xml friendly, not confused by whitespace anywhere in the row.
This User Gave Thanks to DGPickett For This Post:
# 3  
Old 04-13-2011
@DGPickett

where you $newvalue i actually need this value to be pulled from another file.

Basically i have a second file with say 10 rows containing value1, value2 etc. I need (for every row where type is a or b) to update with row[x] data of the second file. Is that also possible with sed?

thanks in advance.
# 4  
Old 04-13-2011
Well, it is a sort of join, but for only 10 or so, generate the sed script from that file, then run it on the other, longer file. You can write the sed substitutes in sed. Note, writing code that writes code makes you no longer a newby, if you succeed.

There might be tools that can interpret both files in SQL and do the join, but cartesian join is slow. UNIX join can do this, too. Sorted merge is nicer, like a many to one sorted join. I wrote a tool for that, like join, but pipe friendly and exploiting knowing file a is always the many side. Google for m1join.c; I think it is in here somewhere.

Finally, some tools like bash, awk have what I call associative arrays, really two column tables so you can look up using a unique key a string, with loading syntax something like: xtable["ardvark"]="First animal." You can load file 2 into such a container and then use it to decide what to substitute. If the shell uses a nice algorythm like hash map or tree, or the set is small, the performance can be good.

Joins - just the start!
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 pull multiple XML tags from the same XML file in Shell.?

I'm searching for the names of a TV show in the XML file I've attached at the end of this post. What I'm trying to do now is pull out/list the data from each of the <SeriesName> tags throughout the document. Currently, I'm only able to get data the first instance of that XML field using the... (9 Replies)
Discussion started by: hungryd
9 Replies

2. UNIX for Beginners Questions & Answers

Grepping multiple XML tag results from XML file.

I want to write a one line script that outputs the result of multiple xml tags from a XML file. For example I have a XML file which has below XML tags in the file: <EMAIL>***</EMAIL> <CUSTOMER_ID>****</CUSTOMER_ID> <BRANDID>***</BRANDID> Now I want to grep the values of all these specified... (1 Reply)
Discussion started by: shubh752
1 Replies

3. Shell Programming and Scripting

Splitting a single xml file into multiple xml files

Hi, I'm having a xml file with multiple xml header. so i want to split the file into multiple files. Sample.xml consists multiple headers so how can we split these multiple headers into multiple files in unix. eg : <?xml version="1.0" encoding="UTF-8"?> <ml:individual... (3 Replies)
Discussion started by: Narendra921631
3 Replies

4. Shell Programming and Scripting

Split xml file into multiple xml based on letterID

Hi All, We need to split a large xml into multiple valid xml with same header(2lines) and footer(last line) for N number of letterId. In the example below we have first 2 lines as header and last line as footer.(They need to be in each split xml file) Header: <?xml version="1.0"... (5 Replies)
Discussion started by: vx04
5 Replies

5. Shell Programming and Scripting

Comparing delta values of one xml file in other xml file

Hi All, I have two xml files. One is having below input <NameValuePair> <name>Daemon</name> <value>tcp:7474</value> </NameValuePair> <NameValuePair> <name>Network</name> <value></value> </NameValuePair> ... (2 Replies)
Discussion started by: sharsour
2 Replies

6. Shell Programming and Scripting

Help required in Splitting a xml file into multiple and appending it in another .xml file

HI All, I have to split a xml file into multiple xml files and append it in another .xml file. for example below is a sample xml and using shell script i have to split it into three xml files and append all the three xmls in a .xml file. Can some one help plz. eg: <?xml version="1.0"?>... (4 Replies)
Discussion started by: ganesan kulasek
4 Replies

7. Shell Programming and Scripting

How to add the multiple lines of xml tags before a particular xml tag in a file

Hi All, I'm stuck with adding multiple lines(irrespective of line number) to a file before a particular xml tag. Please help me. <A>testing_Location</A> <value>LA</value> <zone>US</zone> <B>Region</B> <value>Russia</value> <zone>Washington</zone> <C>Country</C>... (0 Replies)
Discussion started by: mjavalkar
0 Replies

8. UNIX for Dummies Questions & Answers

ftp - check if file on remote exists (skip overriding)

Hello, I have a script that uploads a file from local to remote place using ftp. The problem is that, if on remote host there is a file called the same as the one I want to upload, the ftp program overrides that file. But I don't want to override nothing (even if the remote file is older,... (3 Replies)
Discussion started by: spiriad
3 Replies

9. Shell Programming and Scripting

How to remove xml namespace from xml file using shell script?

I have an xml file: <AutoData xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <Table1> <Data1 10 </Data1> <Data2 20 </Data2> <Data3 40 </Data3> <Table1> </AutoData> and I have to remove the portion xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" only. I tried using sed... (10 Replies)
Discussion started by: Gary1978
10 Replies

10. IP Networking

overriding /etc/resolv.conf with .nslookuprc file

Hi, I have come across a fact that /etc/resolv.conf can be overrided by using ~/.nslookuprc. Is this completely true...i.e can I completely override /etc/resolv.conf. If so, can I have a list of my own domainnames & Nameservers in ~/.nslookuprc that can override those in resolv.conf. ... (3 Replies)
Discussion started by: smanu
3 Replies
Login or Register to Ask a Question