XML data replacement

I have below lines in a file

 <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

<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

<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>
but not sure how to remove the newline characters inside a particular tag alone
something like  <SubRecord> .*ABCD.*ABCD.*</SubRecord>

How should it tell that subrecord apart from any of the others?

Data will always be same in this particular tag which I want to replace, Even if repeats let it be replaced

<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

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

$
1 Like

Another awk approach:

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
1 Like

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

Which one did?

How about this for your later sed convenience:

awk 'gsub(/[[:space:]]+/,"") && length { print $0 RS }' RS="</SubRecord>" infile

Hi Corona688 you code worked but it looks like it was repalcing only in the second module of the tag.
Code provided by Yoda replaces the lines and the following lines which can be tuned to get my requirement.

But thanks a lot, each post is giving me lot of new stuffs to learn..