Shell or perl script to replace XML text in bulk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Shell or perl script to replace XML text in bulk
# 1  
Old 11-28-2012
Shell or perl script to replace XML text in bulk

Hi,
I am looking for assistance over shell or perl (without XML twig module) which replace string in XML file under particular branch..example of code file sample..

Exact requirment : Replace "Su saldo es" in below file with "Your balance" but only in XML branch of Text id=98 and Text Id=12 Not globally.

==
HTML 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>
# 2  
Old 11-28-2012
With assumptions about your input structure:
Code:
awk '/^<Text Id="(98|12)">/{s=1;print;next}
/^<\/Text>/{s=0;print;next}
s{gsub(/Su saldo /,"Your balance ")}1' file


Last edited by elixir_sinari; 11-28-2012 at 12:21 PM..
# 3  
Old 11-28-2012
getting sysntax error when i execute what you written over XML file

awk: syntax error near line 1 )
awk: bailing out near line 1

When i execute part of awk it give me below output .

root@host1> awk '/^<Text Id="(98|12)">/{s=1;print;next}/^<\/Text>/{s=0;print;next}' test.xml
Code:
</Text>
<Text Id="98">
</Text>
</Text>
<Text Id="12">
</Text>


Last edited by Scott; 11-28-2012 at 01:05 PM.. Reason: Code tags
# 4  
Old 11-28-2012
Use /usr/xpg4/bin/awk or nawk instead of awk.
# 5  
Old 11-28-2012
Well, it works Elixir thanks :-)
But i am wondering if there is way to give list of Text id from external file instead of just 98 and 12 as mentioned in code..

Code:
^<Text Id="(98|12)">/


Last edited by Scott; 11-28-2012 at 01:05 PM.. Reason: Ditto
# 6  
Old 11-28-2012
Code:
cat pattfile
98
12

nawk 'FNR==NR{a[$1];next}
match($0,/^<Text Id="[^"]*">/){if(substr($0,11,RLENGTH-12) in a) s=1;print;next}
/^<\/Text>/{s=0;print;next}
s{gsub(/Su saldo/,"Your balance")}1' pattfile mainfile

This User Gave Thanks to elixir_sinari For This Post:
# 7  
Old 11-28-2012
Hi Elixir,
it work like charm..could you modify the code such a way that it add XML tag (first 2 lines) at start and close at end of file..example of output similiar to this

Code:
<Request Type="XML" Operation="Set">
 <Type> 
<Text Id="98">
<Language id="1">Your balance es $mainAccountBalance1Tiene ademas $dedicatedAccount1Balance1 mensajes de texto</Language>
<Language id="2"></Language>
<Language id="3">Your balance es $mainAccountBalance1Tiene ademas $dedicatedAccount1Balance1 mensajes de texto</Language>
<Language id="4">Your balance 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>
<Language id="3">Su saldo es $mainAccountBalance1.</Language>
<Language id="4">Su saldo es $mainAccountBalance1.</Language>
</Text>
<Text Id="12">
<Language id="1">Your balance es $mainAccountBalance1.</Language>
<Language id="2">Your balance  $mainAccountBalance1.</Language>
<Language id="3">Your balance es $mainAccountBalance1.</Language>
<Language id="4">Your balance es $mainAccountBalance1.</Language>
</Text>
</Type>
 </Request>

Thanks again !
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

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

2. Red Hat

How to replace Ip address in .xml file through shell script?

I have one .xml file. which contains the following line. <ParamString StringId="PortAddress" StringValue="172.27.166.170" /> <ParamString StringId="PortAddress" StringValue="172.27.166.171" /> <ParamString StringId="PortAddress" StringValue="172.27.166.202" /> <ParamString... (9 Replies)
Discussion started by: Anjan Ganguly
9 Replies

3. UNIX for Dummies Questions & Answers

Reading XML file and print the values in the text file using Linux shell script

hi guys, i want help... Reding XML file and print the values into the text file using linux shell script file as per below xml file <sequence> <Filename>aldorzum.doc</Filename> <DivisionCode>US</DivisionCode> <ContentType>Template</ContentType> <ProductCode>VIMZIM</ProductCode> </sequence>... (1 Reply)
Discussion started by: sravanreddy
1 Replies

4. Shell Programming and Scripting

perl script to replace the text in the original file

Hi Folks, I have an html file which contains the below line in the body tagI am trying the replace hello with Hello Giridhar programatically. <body> <P><STRONG><FONT face="comic sans ms,cursive,sans-serif"><EM>Hello</EM></FONT></STRONG></P> </body> I have written the below code to... (3 Replies)
Discussion started by: giridhar276
3 Replies

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

6. Shell Programming and Scripting

Loop through text file > Copy Folder > Edit XML files in bulk?

I have a text file which contains lines in this format - it contains 105 lines in total, but I'm just putting 4 here to keep it short: 58571,east_ppl_ppla_por 58788,east_pcy_hd_por 58704,east_pcy_ga_por 58697,east_pcy_pcybs_por It's called id_key.txt I have a sample folder called... (9 Replies)
Discussion started by: biscuitcreek
9 Replies

7. Shell Programming and Scripting

Shell script (not Perl) to parse xml with awk

Hi, I have to make an script according to these: - I have couples of files like: xxxxxxxxxxxxx.csv xxxxxxxxxxxxx_desc.xml - every xml file has diferent fields, but keeps this format: ........ <defaultName>2011-02-25T16:43:43.582Z</defaultName> ........... (2 Replies)
Discussion started by: Pluff
2 Replies

8. Shell Programming and Scripting

Shell Script to replace text

I need a little help with a shell script. I want to be able to specify parameters so that the script searches multiple files (specified as parameters) and only modifies the file if it finds the string of text. For the files it doesn't find the string of text within, it should leave it alone. ... (4 Replies)
Discussion started by: joebaber
4 Replies

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