Scripting question, replacement in xml file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Scripting question, replacement in xml file
# 1  
Old 11-21-2012
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

Code:
<Text Id="98">
 <Language id="1">Su saldo es $mainAccountBalance1Tiene ademas $dedicatedAccount1Balance1 mensajes de texto</Language>
 <Language id="2"></Language>
 <Language id="3">Su saldo es $mainAccountBalance1Tiene ademas $dedicatedAccount1Balance1 mensajes de texto</Language>
 <Language id="4">Su saldo es $mainAccountBalance1Tiene ademas $dedicatedAccount1Balance1 mensajes de texto</Language>
 </Text>
 <Text Id="96"> 
<Language id="1">Su saldo es $mainAccountBalance1.</Language>
 <Language id="2">Su saldo es $mainAccountBalance1.</Language>
 </Text>
 <Text Id="12"> 
<Language id="1">Su saldo es $mainAccountBalance1.</Language>
 <Language id="2">Your balance  $mainAccountBalance1.</Language> 
</Text>


Last edited by Ashu_099; 11-28-2012 at 12:08 PM.. Reason: code tags...
# 2  
Old 11-21-2012
Do not hijack threads of other people - open up your own.
Do use code tags when posting code, data or logs etc.
Do use meaningful subjects, not just "help me...".

Thanks.
This User Gave Thanks to zaxxon For This Post:
# 3  
Old 11-21-2012
Hi Ashu_099,

1.- Replace? with what?
2.- That xml file is not well formed.
3.- Show sample output to get more helpful answers.
This User Gave Thanks to birei For This Post:
# 4  
Old 11-21-2012
Replace with let say "XYZ" but condition is that XML block should begin with particulare Text Id ..

---------- Post updated at 10:48 AM ---------- Previous update was at 10:28 AM ----------

Quote:
Originally Posted by birei
Hi Ashu_099,

1.- Replace? with what?
For example i want to replace "
"Su saldo es" with "your Balance" in XML file ,where XML block start with <Text Id="98"> and <Text Id="12"> ONLY not on other XML blocks.

2.- That xml file is not well formed.
3.- Show sample output to get more helpful answers.
# 5  
Old 11-21-2012
A solution to your problem using perl and the module XML::Twig:
Code:
$ cat infile
<root>
<Text Id="98">
        <Language id="1">Su saldo es $mainAccountBalance1Tiene ademas $dedicatedAccount1Balance1 mensajes de texto</Language>
        <Language id="2"></Language> 
        <Language id="3">Su saldo es $mainAccountBalance1Tiene ademas $dedicatedAccount1Balance1 mensajes de texto</Language> 
        <Language id="4">Su saldo es $mainAccountBalance1Tiene ademas $dedicatedAccount1Balance1 mensajes de texto</Language> 
</Text>  
<Text Id="96"> 
        <Language id="1">Su saldo es $mainAccountBalance1.</Language> 
        <Language id="2">Su saldo es $mainAccountBalance1.</Language> 
</Text>  
<Text Id="12"> 
        <Language id="1">Su saldo es $mainAccountBalance1.</Language> 
        <Language id="2">Your balance  $mainAccountBalance1.</Language> 
</Text>
</root>
$ cat script.pl
#!/usr/bin/perl

use strict;
use warnings;
use XML::Twig;

{
        my $twig = XML::Twig->new(
                twig_handlers => {
                        q{Text[@Id="98"]/Language} => sub {
                                $_->subs_text( qr{(?ix)su \s+ saldo \s+ es \s+ \$mainAccountBalance1Tiene (?=\s)}, 'XYZ' );
                        }
                },
                pretty_print => 'indented',
        )->parsefile( shift )->print;
}
$ perl-5.14.2 script.pl infile
<root>
  <Text Id="98">
    <Language id="1">XYZ ademas $dedicatedAccount1Balance1 mensajes de texto</Language>
    <Language id="2"></Language>
    <Language id="3">XYZ ademas $dedicatedAccount1Balance1 mensajes de texto</Language>
    <Language id="4">XYZ ademas $dedicatedAccount1Balance1 mensajes de texto</Language>
  </Text>
  <Text Id="96">
    <Language id="1">Su saldo es $mainAccountBalance1.</Language>
    <Language id="2">Su saldo es $mainAccountBalance1.</Language>
  </Text>
  <Text Id="12">
    <Language id="1">Su saldo es $mainAccountBalance1.</Language>
    <Language id="2">Your balance  $mainAccountBalance1.</Language>
  </Text>
</root>

This User Gave Thanks to birei For This Post:
# 6  
Old 11-21-2012
Thanks for Quick Response,Can you modify the perl script so that the output look like this

Code:
<root>
 <Text Id="98">         
<Language id="1">Your Balance $mainAccountBalance1Tiene ademas $dedicatedAccount1Balance1 mensajes de texto</Language>         
<Language id="2"></Language>
 <Language id="3">Your Balance $mainAccountBalance1Tiene ademas $dedicatedAccount1Balance1 mensajes de texto</Language>
 <Language id="4">Your Balance $mainAccountBalance1Tiene ademas $dedicatedAccount1Balance1 mensajes de texto</Language>
 </Text> 
  <Text Id="96">
 <Language id="1">Su saldo es $mainAccountBalance1.</Language>
 <Language id="2">Su saldo es $mainAccountBalance1.</Language>  
</Text> 
  <Text Id="12"> 
         <Language id="1">Your Balance $mainAccountBalance1.</Language>
 <Language id="2">Your balance  $mainAccountBalance1.</Language> 
 </Text> </root>


Last edited by vbe; 11-23-2012 at 10:22 AM.. Reason: rm icode
# 7  
Old 11-21-2012
I don't know why you changed the text of the bottom. It's not inside <Text Id="98">. Give a try to:
Code:
$ cat script.pl                                                                                                                                                                                                                              
#!/usr/bin/perl                                                                                                                                                                                                                              
                                                                                                                                                                                                                                             
use strict;                                                                                                                                                                                                                                  
use warnings;                                                                                                                                                                                                                                
use XML::Twig;                                                                                                                                                                                                                               
                                                                                                                                                                                                                                             
{                                                                                                                                                                                                                                            
        my $twig = XML::Twig->new(                                                                                                                                                                                                           
                twig_handlers => {                                                                                                                                                                                                           
                        q{Text[@Id="98"]/Language} => sub {                                                                                                                                                                                  
                                $_->subs_text( qr{(?ix)su \s+ saldo \s+ es (?=\s)}, 'Your Balance' );                                                                                                                                        
                        }                                                                                                                                                                                                                    
                },                                                                                                                                                                                                                           
                pretty_print => 'indented',                                                                                                                                                                                                  
        )->parsefile( shift )->print;                                                                                                                                                                                                        
}                                                                                                                                                                                                                                                                                                                                                                                                                                                                               
$ perl-5.14.2 script.pl infile                                                                                                                                                                                                          
<root>                                                                                                                                                                                                                                       
  <Text Id="98">                                                                                                                                                                                                                             
    <Language id="1">Your Balance $mainAccountBalance1Tiene ademas $dedicatedAccount1Balance1 mensajes de texto</Language>                                                                                                                   
    <Language id="2"></Language>                                                                                                                                                                                                             
    <Language id="3">Your Balance $mainAccountBalance1Tiene ademas $dedicatedAccount1Balance1 mensajes de texto</Language>                                                                                                                   
    <Language id="4">Your Balance $mainAccountBalance1Tiene ademas $dedicatedAccount1Balance1 mensajes de texto</Language>                                                                                                                   
  </Text>                                                                                                                                                                                                                                    
  <Text Id="96">                                                                                                                                                                                                                             
    <Language id="1">Su saldo es $mainAccountBalance1.</Language>                                                                                                                                                                            
    <Language id="2">Su saldo es $mainAccountBalance1.</Language>                                                                                                                                                                            
  </Text>                                                                                                                                                                                                                                    
  <Text Id="12">                                                                                                                                                                                                                             
    <Language id="1">Su saldo es $mainAccountBalance1.</Language>                                                                                                                                                                            
    <Language id="2">Your balance  $mainAccountBalance1.</Language>
  </Text>
</root>

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

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... (8 Replies)
Discussion started by: ratheeshjulk
8 Replies

2. Shell Programming and Scripting

How to extract data from XML file using shell scripting?

Hi , I have input file as XML. following are input data #complex.xml Code: <?xml version="1.0" encoding="UTF-8"?><TEST_doc xmlns="http://www.w3.org/2001/XMLSchema-instance"> <ENTRY uid="123456"> <protein> <name>PROT001</name> <organism>Human</organism> ... (1 Reply)
Discussion started by: arun_kohan
1 Replies

3. Shell Programming and Scripting

How to extract data from xml file using shell scripting?

Hi evry1, This is my 1st post in this forum.Pls help me I want to extract some data froma xml file which has 2000 lines using shell scripting. Actually my xml file has some "audio and video codes" which i need to arrange in a column wise format after extracting it using shell scripting.I... (4 Replies)
Discussion started by: arun_kohan
4 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. UNIX for Dummies Questions & Answers

Delete a record in a xml file using shell scripting

find pattern, delete line with pattern and 3 lines above and 8 lines below the pattern. The pattern is "isup". The entire record with starting tag <record> and ending tag </record> containing the pattern is to be deleted and the rest to be retained. <record> ... (4 Replies)
Discussion started by: sdesstp
4 Replies

7. Solaris

XML to Text file Parsing Using shell scripting

Hi, I want to parse an XML File using Shell Script preferably by using awk command, I/P file is : <gn:ExternalGsmCell id="016P3A"> <gn:attributes> <gn:mnc>410</gn:mnc> <gn:mcc>310</gn:mcc> <gn:lac>8016</gn:lac> ... (2 Replies)
Discussion started by: tech_frk
2 Replies

8. Shell Programming and Scripting

XML to Text file Parsing Using shell scripting

Hi folks, Need some help with XML to text file parsing , the following is the content of the XML File. <xn:SubNetwork id="SNJNPRZDCR0R03"> <xn:MeContext id="PRSJU0005"> <xn:VsDataContainer id="PRSJU0005"> <xn:attributes> ... (6 Replies)
Discussion started by: tech_frk
6 Replies

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

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