XML data replacement


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting XML data replacement
# 1  
Old 02-04-2014
XML data replacement

I have below lines in a file
Code:
 <SubRecord>
    <Property Name="Name">QQQQQQQQ</Property>
    <Property Name="Prompt">YYYYYYYYYY</Property>
    <Property Name="Default">$ddd</Property>
    <Property Name="HelpTxt">blahblah</Property>
    <Property Name="ParamType">4</Property>
    <Property Name="ParamLength">0</Property>
    <Property Name="ParamScale">0</Property>
 </SubRecord>
<SubRecord>
  <Property Name="Name">$ABCD</Property>
  <Property Name="Prompt">ABCD</Property>
  <Property Name="Default">$ddd</Property>
  <Property Name="ParamType">0</Property>
  <Property Name="ParamLength">0</Property>
  <Property Name="ParamScale">0</Property>
</SubRecord>
 <SubRecord>
    <Property Name="Name">$XYZZ</Property>
    <Property Name="Prompt">XYZZ</Property>
    <Property Name="Default">$ddd</Property>
    <Property Name="ParamType">0</Property>
    <Property Name="ParamLength">0</Property>
    <Property Name="ParamScale">0</Property>
 </SubRecord>

in this file the piece in red should be replacd with below data
Code:
<SubRecord>
  <Property Name="Name">$XXXXXX</Property>
  <Property Name="Prompt">XXXXXX</Property>
  <Property Name="Default">sasasasasa</Property>
  <Property Name="ParamType">1</Property>
  <Property Name="ParamLength">0</Property>
  <Property Name="ParamScale">0</Property>
</SubRecord>


I am trying to bring the required tag alone into one single line and then use sed to replace the data
Code:
<SubRecord>
  <Property Name="Name">$ABCD</Property>
  <Property Name="Prompt">ABCD</Property>
  <Property Name="Default">$ddd</Property>
  <Property Name="ParamType">0</Property>
  <Property Name="ParamLength">0</Property>
  <Property Name="ParamScale">0</Property>
</SubRecord>

Code:
but not sure how to remove the newline characters inside a particular tag alone
something like  <SubRecord> .*ABCD.*ABCD.*</SubRecord>


Last edited by ratheeshjulk; 02-04-2014 at 05:21 PM.. Reason: Corrcted typo
# 2  
Old 02-04-2014
How should it tell that subrecord apart from any of the others?
# 3  
Old 02-04-2014
Data will always be same in this particular tag which I want to replace, Even if repeats let it be replaced
Code:
<SubRecord>  <Property Name="Name">$ABCD</Property>  <Property Name="Prompt">ABCD</Property>  <Property Name="Default">$ddd</Property>  <Property Name="ParamType">0</Property>  <Property Name="ParamLength">0</Property>  <Property Name="ParamScale">0</Property></SubRecord>

can be replaced by
Code:
<SubRecord>  <Property Name="Name">$XXXXXX</Property>  <Property Name="Prompt">XXXXXX</Property>  <Property Name="Default">sasasasasa</Property>  <Property Name="ParamType">1</Property>  <Property Name="ParamLength">0</Property>  <Property Name="ParamScale">0</Property></SubRecord>

# 4  
Old 02-04-2014
Code:
$ cat subrecord.awk

BEGIN { RS="<"; ORS="<"; P=1 }

NR==1 { printf("%s",ORS); }
FNR==1 && (!$1) { next }

NR==FNR { D[++L]=$0; next }
/^SubRecord>/ {
        T++;
        if(T == 2)
        {
                for(X=1; X<L; X++) print D[X]
                P=0
                next
        }
}

/^\/SubRecord/ && (!P) { P=1 }
P { print $0 }

$ awk -f subrecord.awk replacementdata datafile > outputfile

<SubRecord>
    <Property Name="Name">QQQQQQQQ</Property>
    <Property Name="Prompt">YYYYYYYYYY</Property>
    <Property Name="Default">$ddd</Property>
    <Property Name="HelpTxt">blahblah</Property>
    <Property Name="ParamType">4</Property>
    <Property Name="ParamLength">0</Property>
    <Property Name="ParamScale">0</Property>
 </SubRecord>
<SubRecord>
  <Property Name="Name">$ABCD</Property>
  <Property Name="Prompt">ABCD</Property>
  <Property Name="Default">$ddd</Property>
  <Property Name="ParamType">0</Property>
  <Property Name="ParamLength">0</Property>
  <Property Name="ParamScale">0</Property>
</SubRecord>
 <SubRecord>
    <Property Name="Name">$XYZZ</Property>
    <Property Name="Prompt">XYZZ</Property>
    <Property Name="Default">$ddd</Property>
    <Property Name="ParamType">0</Property>
    <Property Name="ParamLength">0</Property>
    <Property Name="ParamScale">0</Property>
 </SubRecord>

$

This User Gave Thanks to Corona688 For This Post:
# 5  
Old 02-04-2014
Another awk approach:
Code:
awk '
        !(n) {
                n = sub ( />\$ABCD</, ">$XXXXXX<" )
        }
        n {
                sub ( />ABCD</, ">XYZZ<" )
                sub ( />\$ddd</, ">sasasasasa<" )
                sub ( /ParamType\">0/, "ParamType\">1" )
        }
        /ParamScale/ && n {
                n = 0
        }
        1
' file.xml

This User Gave Thanks to Yoda For This Post:
# 6  
Old 02-04-2014
Thanks for the quick help. I hope this code is replacing the second para within the tab "SubRecord" with the replacement string, which will not serve my need.
Is there a way we can delete all the new line characters between the tags <SubRecord> and </SubRecord> then i can replace the entire records using sed..

---------- Post updated at 05:39 PM ---------- Previous update was at 05:27 PM ----------

This one worked like charm.. Thanks a lot.......
# 7  
Old 02-04-2014
Which one did?
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Programming

Resample time series data with replacement any way to do this in awk or just bash script

I have some time series data that I need to resample or downsample at some specific time intervals. The firs column is time in decimal hours. I am tryiong to resample this data every 3 minutse. So I need a data value ever 0.05. Here is the example data and as you can see, there time slot for 0.1500... (3 Replies)
Discussion started by: malandisa
3 Replies

2. Shell Programming and Scripting

Pulling data from xml

Hi there, Please could anyone help with this. I have an xml file that contains repeating values eg <Rule name> AAAAA <Action> BBBBB </Action> <Data> CCCCC </Data> <Type> DDDDD </Type> </Rule name> <Rule name> A1A1A1A1 <Action> B1B1B1B1 </Action> <Data> C1C1C1C </Data> <Type>... (4 Replies)
Discussion started by: ssideel
4 Replies

3. Shell Programming and Scripting

Parsing XML (and insert data) then output data (bash / Solaris)

Hi folks I have a script I wrote that basically parses a bunch of config and xml files works out were to add in the new content then spits out the data into a new file. It all works - apart from the xml and config file format in the new file with XML files the original XML (that ends up in... (2 Replies)
Discussion started by: dfinch
2 Replies

4. HP-UX

XML tag name content replacement

Hi, Need to replace an XML tag name contents, please provide any suggestions. Scenario is : <abc_def>Value_some_content</abc_def> Expected output : <abc:def>Value_some_content</abc:def> We have many tag with different names & contents in a file or a string. Please help on the... (3 Replies)
Discussion started by: periyasamycse
3 Replies

5. Shell Programming and Scripting

XML id replacement with shell

Hi.. I have a problem with replacing of id-0f5435080b with some name daily, problem here is whenever I generate xml file it generates unique id for instance say for example today id-0f5435080b and tomorrow it may be id-0f68643508so basically I just want to replace this id with some name say... (5 Replies)
Discussion started by: nex_asp
5 Replies

6. Shell Programming and Scripting

Scripting question, replacement in xml file

Hi Anybody can help me to make a script that replace string "Su saldo es" with "Your balance" in XML block if it start with Text Id=98 and Text Id= 12 only sample of part of file below <Text Id="98"> <Language id="1">Su saldo es $mainAccountBalance1Tiene ademas $dedicatedAccount1Balance1... (13 Replies)
Discussion started by: Ashu_099
13 Replies

7. Shell Programming and Scripting

Data between XML Tags

<?xml version="1.0" encoding="iso-8859-1" ?> <TABLE> <TEST> <ID> 123 </ID> <name> abc </name> </TEST> <TEST> <ID> 123 </ID> <name> abc2 </name> </TEST> </TABLE> <TABLE> <TEST> <ID> 456 </ID> <name> def </name> </TEST> <TEST> ... (8 Replies)
Discussion started by: eskay
8 Replies

8. Shell Programming and Scripting

XML tag replacement from different XML file

We have 2 XML file 1. ORIGINAL.xml file and 2. ATTRIBUTE.xml files, In the ORIGINAL.xml we need some modification as <resourceCode>431048</resourceCode>under <item type="Manufactured"> tag - we need to grab the 431048 value from tag and pass it to database table in unix shell script to find the... (0 Replies)
Discussion started by: balrajg
0 Replies

9. Shell Programming and Scripting

sed xml file multiple line replacement

I have a file called config.xml, it's a simple xml file, and I need use sed/awk to erase some lines. <machine xsi:type="unix-machineType"> <name>server1</name> <node-manager> <name>server1</name> <listen-address>server1</listen-address> </node-manager> ... (3 Replies)
Discussion started by: cbo0485
3 Replies
Login or Register to Ask a Question