Replace text inside XML file based on condition

Hi All,

I want to change the name as SEQ_13
ie., <Property Name="Name">SEQ_13</Property>
when the Stage Type is PxSequentialFile
ie., <Property Name="StageType">PxSequentialFile</Property> :wall:

Input.XML

 
<Main>
      <Record Identifier="V0S13" Type="CustomStage" Readonly="0">
         <Property Name="Name">Sequential_File_13</Property>
         <Property Name="NextID">2</Property>
         <Property Name="InputPins">V0S13P1</Property>
         <Property Name="StageType">PxSequentialFile</Property>
         <Property Name="AllowColumnMapping">0</Property>
         <Property Name="NextRecordID">0</Property>
      </Record>
        ......
         ......
 
</Main>

Output.XML

<Main>
      <Record Identifier="V0S13" Type="CustomStage" Readonly="0">
         <Property Name="Name">SEQ_13</Property>
         <Property Name="NextID">2</Property>
         <Property Name="InputPins">V0S13P1</Property>
         <Property Name="StageType">PxSequentialFile</Property>
         <Property Name="AllowColumnMapping">0</Property>
         <Property Name="NextRecordID">0</Property>
      </Record>
        ......
         ......
 
</Main>

Thanks in advance

A perlish solution:

#!/usr/bin/perl 

$/ = '</Record>' . $/;

while (<>) {
    if (m{<Property Name="StageType">PxSequentialFile</Property>}) {
        s{<Property Name="Name">[^<]+</Property>}
         {<Property Name="Name">SEQ_13</Property>};
    }

    print;
}

I am quite sure there are many, many other ways to solve this.

Try this...

sed -n '/<\/Record>/!{H};/<\/Record>/{x;/PxSequentialFile/{s/Sequential_File_13/SEQ_13/};p};${x;p}' input_file

If it works for you, then use -i option to make the changes in the file directly using sed.

--ahamed

@Ahamed,

I do so need to improve my sed-chops.
Thanks,

M.D.L.