Replace text inside XML file based on condition


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Replace text inside XML file based on condition
# 1  
Old 11-25-2011
Lightbulb Replace text in XML file based on Previous Line

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

Input.XML
Code:
 
<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
Code:
<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

Last edited by kmsekhar; 11-25-2011 at 08:22 AM..
# 2  
Old 11-25-2011
A perlish solution:
Code:
#!/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.
# 3  
Old 11-25-2011
Try this...
Code:
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

Last edited by ahamed101; 11-25-2011 at 10:23 AM..
# 4  
Old 11-29-2011
@Ahamed,

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

M.D.L.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Manipulate condition to send mail based on output text in file

Hi All, I have a working script as below. echo "Files loaded with $(cat /var/tmp/script.X1.out)" | mail -s "Files loaded with return code" mailid This script takes the output from script.X1.out file and appends the text "Files loaded with return code" and sends the email. Now what I want... (5 Replies)
Discussion started by: midhun3108
5 Replies

2. UNIX for Beginners Questions & Answers

Find replace text in xml file on the fly

Dear Unix guru, I have a .XML file which is being used to load data to oracle. This file comes on unix box and one of the tag in xml is oracle key word. I want to find that tag and replace with new tag on the fly For example I will get one of the tag in xml is as below <from>Test Test... (12 Replies)
Discussion started by: guddu_12
12 Replies

3. Shell Programming and Scripting

Help with tag value extraction from xml file based on a matching condition

Hi , I have a situation where I need to search an xml file for the presence of a tag <FollowOnFrom> and also , presence of partial part of the following tag <ContractRequest _LoadId and if these 2 exist ,then extract the value from the following tag <_LocalId> which is "CW2094139". There... (2 Replies)
Discussion started by: paul1234
2 Replies

4. Shell Programming and Scripting

Copy and paste text inside a xml file

I have a really big XML file. I need copy the value of one tag inside another one tag. I try to publish one example. <channel update="i" site="merge-xmltv" site_id="" xmltv_id="Rai 1">Rai 1</channel> <channel update="i" site="merge-xmltv" site_id="" xmltv_id="Rai 1 +2HD">Rai 1... (6 Replies)
Discussion started by: Tapiocapioca
6 Replies

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

6. Shell Programming and Scripting

Help with File processing - Adding predefined text to particular record based on condition

I am generating a output: Name Count_1 Count_2 abc 12 12 def 15 14 ghi 16 16 jkl 18 18 mno 7 5 I am sending the output in html email, I want to add the code: <font color="red"> NAME COLUMN record </font> for the Name... (8 Replies)
Discussion started by: karumudi7
8 Replies

7. Shell Programming and Scripting

extract xml tag based on condition

Hi All, I have a large xml file of invoices. The file looks like below: <INVOICES> <INVOICE> <NAME>Customer A</NAME> <INVOICE_NO>1234</INVOICE_NO> </INVOICE> <INVOICE> <NAME>Customer A</NAME> <INVOICE_NO>2345</INVOICE_NO> </INVOICE> <INVOICE> <NAME>Customer A</NAME>... (9 Replies)
Discussion started by: angshuman
9 Replies

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

9. Shell Programming and Scripting

pattern replace inside text file using sed

Hi, I have a situation where I want to replace some occurrences of ".jsp" into ".html" inside a text file. For Example: If a pattern found like <a href="http://www.mysite.com/mypage.jsp"> it should be retained. But if a pattern found like <a href="../mypage.jsp"> it should be changed to... (4 Replies)
Discussion started by: meharo
4 Replies

10. Shell Programming and Scripting

how to replace a text inside a file based on a xml key

<c-param> <param-name>Number</param-name> <param-value>22</param-value> <description>my house number</description> </c-param> <c-param> <param-name>Address</param-name> ... (4 Replies)
Discussion started by: reldb
4 Replies
Login or Register to Ask a Question