Replace Char in XML Base on Condition


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Replace Char in XML Base on Condition
# 1  
Old 03-20-2014
Replace Char in XML Base on Condition

HI All

Here is my Input file A.

I want to add extra char in the line base on below condition.

1> if Below second line is
Code:
<xn:vsDataType>vsDataEUtranCellFDD</xn:vsDataType>

then
Code:
<xn:VsDataContainer id=

will be
Code:
<xn:VsDataContainer_id_1=

2 > if Below second line is
Code:
<xn:vsDataType>vsDataEUtranFreqRelation</xn:vsDataType>

then this <xn:VsDataContainer id= will be
Code:
<xn:VsDataContainer_id_2=

3 > if Below second line is
Code:
<xn:vsDataType>vsDataEUtranCellRelation</xn:vsDataType>

then this
Code:
<xn:VsDataContainer id=

will be
Code:
<xn:VsDataContainer_id_3=

Input and changes highlighted in color


Input :-

Code:
<?xml version="1.0" encoding="UTF-8"?>
<bulkCmConfigDataFile xmlns:un="utranNrm.xsd"
    xmlns:es="EricssonSpecificAttributes.13.25.xsd"
    xmlns:xn="genericNrm.xsd" xmlns:gn="geranNrm.xsd" xmlns="configData.xsd">
    <fileHeader fileFormatVersion="32.615 V4.5" vendorName="Ericsson"/>
    <configData dnPrefix="Undefined">
        <xn:SubNetwork id="ONRM_ROOT_MO_R">
            <xn:MeContext id="CCL01736">
                <xn:ManagedElement id="1">
                    <xn:VsDataContainer id="1">
                        <xn:attributes>
                            <xn:vsDataType>vsDataENodeBFunction</xn:vsDataType>
                            <xn:vsDataFormatVersion>EricssonSpecificAttributes.13.25</xn:vsDataFormatVersion>
                            <es:vsDataENodeBFunction/>
                        </xn:attributes>
                        <xn:VsDataContainer id="CCL01736_7A_1">
                            <xn:attributes>
                                <xn:vsDataType>vsDataEUtranCellFDD</xn:vsDataType>
                                <xn:vsDataFormatVersion>EricssonSpecificAttributes.13.25</xn:vsDataFormatVersion>
                                <es:vsDataEUtranCellFDD/>
                            </xn:attributes>
                            <xn:VsDataContainer id="5780">
                                <xn:attributes>
                                    <xn:vsDataType>vsDataEUtranFreqRelation</xn:vsDataType>
                                    <xn:vsDataFormatVersion>EricssonSpecificAttributes.13.25</xn:vsDataFormatVersion>
                                    <es:vsDataEUtranFreqRelation/>
                                </xn:attributes>
                                <xn:VsDataContainer id="310410-660010-15">
                                    <xn:attributes>
                                    <xn:vsDataType>vsDataEUtranCellRelation</xn:vsDataType>
                                    <xn:vsDataFormatVersion>EricssonSpecificAttributes.13.25</xn:vsDataFormatVersion>
                                    <es:vsDataEUtranCellRelation>
                                    <es:cellIndividualOffsetEUtran>0</es:cellIndividualOffsetEUtran>
                                    <es:loadBalancing>0</es:loadBalancing>
                                    </es:vsDataEUtranCellRelation>
                                    </xn:attributes>
                                </xn:VsDataContainer>
                                <xn:VsDataContainer id="310410-660010-16">
                                    <xn:attributes>
                                    <xn:vsDataType>vsDataEUtranCellRelation</xn:vsDataType>
                                    <xn:vsDataFormatVersion>EricssonSpecificAttributes.13.25</xn:vsDataFormatVersion>
                                    <es:vsDataEUtranCellRelation>
                                    <es:cellIndividualOffsetEUtran>0</es:cellIndividualOffsetEUtran>
                                   <es:loadBalancing>0</es:loadBalancing>
                                    </es:vsDataEUtranCellRelation>
                                    </xn:attributes>
                                </xn:VsDataContainer>
   			    </xn:VsDataContainer>
                        </xn:VsDataContainer>
			<xn:VsDataContainer id="CCL01736_9A_1">
                            <xn:attributes>
                                <xn:vsDataType>vsDataEUtranCellFDD</xn:vsDataType>
                                <xn:vsDataFormatVersion>EricssonSpecificAttributes.13.25</xn:vsDataFormatVersion>
                                <es:vsDataEUtranCellFDD/>
                            </xn:attributes>
                            <xn:VsDataContainer id="5780">
                                <xn:attributes>
                                    <xn:vsDataType>vsDataEUtranFreqRelation</xn:vsDataType>
                                    <xn:vsDataFormatVersion>EricssonSpecificAttributes.13.25</xn:vsDataFormatVersion>
                                    <es:vsDataEUtranFreqRelation/>
                                </xn:attributes>
                                <xn:VsDataContainer id="CCL01736_7A_1">
                                    <xn:attributes>
                                    <xn:vsDataType>vsDataEUtranCellRelation</xn:vsDataType>
                                    <xn:vsDataFormatVersion>EricssonSpecificAttributes.13.25</xn:vsDataFormatVersion>
                                    <OfLastModification>
                                    <es:loadBalancing>0</es:loadBalancing>
                                    </es:vsDataEUtranCellRelation>
                                    </xn:attributes>
                                </xn:VsDataContainer>
                            </xn:VsDataContainer>
                        </xn:VsDataContainer>
                        <xn:VsDataContainer id="CCL01736_9C_1">
                            <xn:attributes>
                                <xn:vsDataType>vsDataEUtranCellFDD</xn:vsDataType>
                                <xn:vsDataFormatVersion>EricssonSpecificAttributes.13.25</xn:vsDataFormatVersion>
                                <es:vsDataEUtranCellFDD/>
                            </xn:attributes>
                            <xn:VsDataContainer id="5780">
                                <xn:attributes>
                                    <xn:vsDataType>vsDataEUtranFreqRelation</xn:vsDataType>
                                    <xn:vsDataFormatVersion>EricssonSpecificAttributes.13.25</xn:vsDataFormatVersion>
                                    <es:vsDataEUtranFreqRelation/>
                                </xn:attributes>
                                <xn:VsDataContainer id="CCL01736_7C_1">
                                    <xn:attributes>
                                    <xn:vsDataType>vsDataEUtranCellRelation</xn:vsDataType>
                                    <xn:vsDataFormatVersion>EricssonSpecificAttributes.13.25</xn:vsDataFormatVersion>
                                    <es:vsDataEUtranCellRelation>
                                    <es:cellIndividualOffsetEUtran>0</es:cellIndividualOffsetEUtran>
                                    <es:loadBalancing>0</es:loadBalancing>
                                    </es:vsDataEUtranCellRelation>
                                    </xn:attributes>
                                </xn:VsDataContainer>
                            </xn:VsDataContainer>
                        </xn:VsDataContainer>
                    </xn:VsDataContainer>
                </xn:ManagedElement>
            </xn:MeContext>
         </xn:SubNetwork>
    </configData>
    <fileFooter dateTime="2014-03-19T14:36:50Z"/>
</bulkCmConfigDataFile>


Output :-

Code:
<?xml version="1.0" encoding="UTF-8"?>
<bulkCmConfigDataFile xmlns:un="utranNrm.xsd"
    xmlns:es="EricssonSpecificAttributes.13.25.xsd"
    xmlns:xn="genericNrm.xsd" xmlns:gn="geranNrm.xsd" xmlns="configData.xsd">
    <fileHeader fileFormatVersion="32.615 V4.5" vendorName="Ericsson"/>
    <configData dnPrefix="Undefined">
        <xn:SubNetwork id="ONRM_ROOT_MO_R">
            <xn:MeContext id="CCL01736">
                <xn:ManagedElement id="1">
                    <xn:VsDataContainer id="1">
                        <xn:attributes>
                            <xn:vsDataType>vsDataENodeBFunction</xn:vsDataType>
                            <xn:vsDataFormatVersion>EricssonSpecificAttributes.13.25</xn:vsDataFormatVersion>
                            <es:vsDataENodeBFunction/>
                        </xn:attributes>
                        <xn:VsDataContainer_id_1="CCL01736_7A_1">
                            <xn:attributes>
                                <xn:vsDataType>vsDataEUtranCellFDD</xn:vsDataType>
                                <xn:vsDataFormatVersion>EricssonSpecificAttributes.13.25</xn:vsDataFormatVersion>
                                <es:vsDataEUtranCellFDD/>
                            </xn:attributes>
                            <xn:VsDataContainer_id_2="5780">
                                <xn:attributes>
                                    <xn:vsDataType>vsDataEUtranFreqRelation</xn:vsDataType>
                                    <xn:vsDataFormatVersion>EricssonSpecificAttributes.13.25</xn:vsDataFormatVersion>
                                    <es:vsDataEUtranFreqRelation/>
                                </xn:attributes>
                                <xn:VsDataContainer_id_3="310410-660010-15">
                                    <xn:attributes>
                                    <xn:vsDataType>vsDataEUtranCellRelation</xn:vsDataType>
                                    <xn:vsDataFormatVersion>EricssonSpecificAttributes.13.25</xn:vsDataFormatVersion>
                                    <es:vsDataEUtranCellRelation>
                                    <es:cellIndividualOffsetEUtran>0</es:cellIndividualOffsetEUtran>
                                    <es:loadBalancing>0</es:loadBalancing>
                                    </es:vsDataEUtranCellRelation>
                                    </xn:attributes>
                                </xn:VsDataContainer>
                                <xn:VsDataContainer_id_3="310410-660010-16">
                                    <xn:attributes>
                                    <xn:vsDataType>vsDataEUtranCellRelation</xn:vsDataType>
                                    <xn:vsDataFormatVersion>EricssonSpecificAttributes.13.25</xn:vsDataFormatVersion>
                                    <es:vsDataEUtranCellRelation>
                                    <es:cellIndividualOffsetEUtran>0</es:cellIndividualOffsetEUtran>
                                   <es:loadBalancing>0</es:loadBalancing>
                                    </es:vsDataEUtranCellRelation>
                                    </xn:attributes>
                                </xn:VsDataContainer>
   			    </xn:VsDataContainer>
                        </xn:VsDataContainer>
			<xn:VsDataContainer_id_1="CCL01736_9A_1">
                            <xn:attributes>
                                <xn:vsDataType>vsDataEUtranCellFDD</xn:vsDataType>
                                <xn:vsDataFormatVersion>EricssonSpecificAttributes.13.25</xn:vsDataFormatVersion>
                                <es:vsDataEUtranCellFDD/>
                            </xn:attributes>
                            <xn:VsDataContainer_id_2="5780">
                                <xn:attributes>
                                    <xn:vsDataType>vsDataEUtranFreqRelation</xn:vsDataType>
                                    <xn:vsDataFormatVersion>EricssonSpecificAttributes.13.25</xn:vsDataFormatVersion>
                                    <es:vsDataEUtranFreqRelation/>
                                </xn:attributes>
                                <xn:VsDataContainer_id_3="CCL01736_7A_1">
                                    <xn:attributes>
                                    <xn:vsDataType>vsDataEUtranCellRelation</xn:vsDataType>
                                    <xn:vsDataFormatVersion>EricssonSpecificAttributes.13.25</xn:vsDataFormatVersion>
                                    <OfLastModification>
                                    <es:loadBalancing>0</es:loadBalancing>
                                    </es:vsDataEUtranCellRelation>
                                    </xn:attributes>
                                </xn:VsDataContainer>
                            </xn:VsDataContainer>
                        </xn:VsDataContainer>
                        <xn:VsDataContainer_id_1="CCL01736_9C_1">
                            <xn:attributes>
                                <xn:vsDataType>vsDataEUtranCellFDD</xn:vsDataType>
                                <xn:vsDataFormatVersion>EricssonSpecificAttributes.13.25</xn:vsDataFormatVersion>
                                <es:vsDataEUtranCellFDD/>
                            </xn:attributes>
                            <xn:VsDataContainer_id_2="5780">
                                <xn:attributes>
                                    <xn:vsDataType>vsDataEUtranFreqRelation</xn:vsDataType>
                                    <xn:vsDataFormatVersion>EricssonSpecificAttributes.13.25</xn:vsDataFormatVersion>
                                    <es:vsDataEUtranFreqRelation/>
                                </xn:attributes>
                                <xn:VsDataContainer_id_3="CCL01736_7C_1">
                                    <xn:attributes>
                                    <xn:vsDataType>vsDataEUtranCellRelation</xn:vsDataType>
                                    <xn:vsDataFormatVersion>EricssonSpecificAttributes.13.25</xn:vsDataFormatVersion>
                                    <es:vsDataEUtranCellRelation>
                                    <es:cellIndividualOffsetEUtran>0</es:cellIndividualOffsetEUtran>
                                    <es:loadBalancing>0</es:loadBalancing>
                                    </es:vsDataEUtranCellRelation>
                                    </xn:attributes>
                                </xn:VsDataContainer>
                            </xn:VsDataContainer>
                        </xn:VsDataContainer>
                    </xn:VsDataContainer>
                </xn:ManagedElement>
            </xn:MeContext>
         </xn:SubNetwork>
    </configData>
    <fileFooter dateTime="2014-03-19T14:36:50Z"/>
</bulkCmConfigDataFile>


Last edited by asavaliya; 03-20-2014 at 11:10 AM..
# 2  
Old 03-20-2014
one way of doing it:
Code:
tac file.xml | awk '
 /vsDataEUtranCellFDD/ {id=1}
 /vsDataEUtranFreqRelation/ {id=2}
 /vsDataEUtranCellRelation/ {id=3}
 /VsDataContainer id/ {sub(/ id=/,"_id_"id"=")}1' | tac

This User Gave Thanks to Subbeh For This Post:
# 3  
Old 03-20-2014
Getting Error

./gs.sh[232]: tac: not found
./gs.sh[232]: tac: not found
# 4  
Old 03-20-2014
Replace tac with tail -r or sed '1!G;h;$!d', see if that works
This User Gave Thanks to Subbeh For This Post:
# 5  
Old 03-20-2014
Now i have used below command to get value but its not Parfect>


Code:
nawk ' /vsDataEUtranCellFDD/ {id=1} /vsDataEUtranFreqRelation/ {id=2} /vsDataEUtranCellRelation/ {id=3} /VsDataContainer id/ {sub(/ id=/,"_id_"id"=")}1' $CS_XML > $CS_XML_1

Its adding id base on previous line.

---------- Post updated at 11:57 AM ---------- Previous update was at 11:52 AM ----------

Still No Luck !!!!

---------- Post updated at 12:15 PM ---------- Previous update was at 11:57 AM ----------

Thanks Guys Its Working !!!!!! Both of you guys Great solution
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Replace \n char in Data

File is pipe delimited with 17 fields. We may get \n char (1 or more \n in one field or multi fileds) in data in any field. Need to replace \n in data with space and not the Ture \n that is line separator. I tried below awk command it did not work as expected. awk '(NR-1)%2{$1=$1}1' RS=\|... (9 Replies)
Discussion started by: rajeshkumare
9 Replies

2. Shell Programming and Scripting

Split Big XML file Base on tag

HI I want to split file base on tag name. I have few header and footer on file <?xml version="1.33" encing="UTF-8"?> <bulkCmConfigDataFile" <xn:SubNetwork id="ONRM_ROOT"> <xn:MeContext id="PPP04156"> ... (4 Replies)
Discussion started by: pareshkp
4 Replies

3. Shell Programming and Scripting

Adding Extra Column in txt file base on Condition

HI Guys, I have below input. Output Base on Below Condition. 1> if forth column is empty and next coming line have same name with \es then add that column name on all rows 2>rest of all are es:vsDataEUtranCellFDD Input:- CCL01736 CCL01736_7A_1 es:vsDataEUtranCellFDD ... (3 Replies)
Discussion started by: pareshkp
3 Replies

4. Shell Programming and Scripting

Check the value in xml based on condition

Hi, I have a log file having n number of xml's like the one below. <uOStatus xmlns:env="http://abc.org/def/ghi/"... (3 Replies)
Discussion started by: Neethu
3 Replies

5. Shell Programming and Scripting

Replace char between chars - help needed

Hello, I have a csv file with "^" as text delimiters and "|" as field delimiters. It's converted from a xls file. One record looks like this: ^Tablete Internet^|Archos|501838|^Tableta Internet ARCHOS 80 G9 ...| ... (more lines) ... "501|838"^|330.00|USD|sl|12|0|Link|^router wireless 150... (10 Replies)
Discussion started by: go0ogl3
10 Replies

6. Shell Programming and Scripting

Find and replace all extended char.

Hi Guys, I wand find and replace all Extended ASCII Codes from all my log files. My Log files: /home/Kalr/PPool/Output i have logs file in sub dir. /home/Kalr/PPool/Output/X /home/Kalr/PPool/Output/Y /home/Kalr/PPool/Output/Z My Abc.log file input: Extended ASCII Codes :€“ ... (4 Replies)
Discussion started by: asavaliya
4 Replies

7. Shell Programming and Scripting

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">... (3 Replies)
Discussion started by: kmsekhar
3 Replies

8. Shell Programming and Scripting

Replace char on text file

Hi , I have problem on replace char on text file #!/bin/bash echo "Enter Third value : " read a sed '2 s/192.160.1.1/cut -d"." -f3/$a/g' tcpip.txt > a.txt I want replace line 2 on tcpip.txt with value a that I input but it's not run Please help me (2 Replies)
Discussion started by: phillipss
2 Replies

9. Shell Programming and Scripting

Remove lines from XML based on condition

Hi, I need to remove some lines from an XML file is the value within a tag is empty. Imagine this scenario, <acd><acdID>2</acdID><logon></logon></acd> <acd><acdID></acdID><logon></logon></acd> <acd><acdID></acdID><logon></logon></acd> <acd><acdID></acdID><logon></logon></acd> I... (3 Replies)
Discussion started by: giles.cardew
3 Replies

10. Shell Programming and Scripting

How to replace any char with newline char.

Hi, How to replace any character in a file with a newline character using sed .. Ex: To replace ',' with newline Input: abcd,efgh,ijkl,mnop Output: abcd efgh ijkl mnop Thnx in advance. Regards, Sasidhar (5 Replies)
Discussion started by: mightysam
5 Replies
Login or Register to Ask a Question

Featured Tech Videos