Find Node and replace line(s) preceding in xml file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Find Node and replace line(s) preceding in xml file
# 1  
Old 05-10-2012
Find Node and replace line(s) preceding in xml file

Hello,

I have an xml file whose contacts are like below:

<Node>Apple
<B>Value1</B>
<B>Value2</B>
<B>Value3</B>
</Node>
<Node>Mango
<B>Value1</B>
<B>Value2</B>
<B>Value3</B>
</Node>
<Node>Apple
<B>Value1</B>
<B>Value2</B>
<B>Value3</B>
</Node>
<Node>Bannana
<B>Value1</B>
<B>Value2</B>
<B>Value3</B>
</Node>

I would like to find/grep Node type "Apple" and than would like to change "value3" to "value4" of Node Apple only in this xml file. Is it possible that I can do it through sed or any other scripting utility?

Any help would be highly appreciated. Thanks in advance!! Smilie

BR,
Umar

Last edited by umarsatti; 05-10-2012 at 01:57 AM..
# 2  
Old 05-10-2012
use awk, if you dont have nawk

Code:
 
$ nawk '/Node/,/<\/Node/{if($0~/Apple/)a=1;}{if($0~/Value3/ && a){sub("Value3","Value4");a=0}}1' test.txt
<Node>Apple
<B>Value1</B>
<B>Value2</B>
<B>Value4</B>
</Node>
<Node>Mango
<B>Value1</B>
<B>Value2</B>
<B>Value3</B>
</Node>
<Node>Apple
<B>Value1</B>
<B>Value2</B>
<B>Value4</B>
</Node>
<Node>Bannana
<B>Value1</B>
<B>Value2</B>
<B>Value3</B>
</Node>

This User Gave Thanks to itkamaraj For This Post:
# 3  
Old 05-10-2012
Hello itkamaraj,

Thank you for your reply, your script perfectly fits into my example. However I am attaching my original file where I would like to change "Accumulator Factor" under node "DiscountOnUsage" only.

<Node>DiscountOnUsage
<Condition>ActiveOffer
<ActiveOffer>401</ActiveOffer>
</Condition>
.
.
.
<Accumulator Factor="3.78333333333333" ID="6"/>
</Tariff>
</Node>

Can you please help in adopting this script as per my above requirement. FYI original file is also attached.

Thanks!!

P.S: For instance I would like to change Accumulator Factor to --> "<Accumulator Factor="3.78333333333333" ID="6"/><Accumulator Factor="3.78333333333333" ID="72"/>

BR,
Umar
# 4  
Old 05-10-2012
How about this ?
Code:
perl -nle 'if(/\<Node\>DiscountOnUsage/ ... /\<\/Node\>/){$_=~s/(\<Accumulator Factor\s*=\s*)(.+?\s+ID\s*=\s*)(.+?)\/\>/$1$2"72"\/\>/g;print $_;}else{print}' zone_IDD.txt

Login or Register to Ask a Question

Previous Thread | Next Thread

10 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. Shell Programming and Scripting

Looping through XML file on basis of a node

<?xml version="1.0" encoding="UTF-8"?> <Document> <FIToFICstmrCdtTrf> <GrpHdr> <MsgId>10001</MsgId> <NbOfTxs>1</NbOfTxs> <IntrBkSttlmDt>2015-05-06</IntrBkSttlmDt> <SttlmInf> <SttlmMtd>CLRG</SttlmMtd> </SttlmInf> <PmtTpInf> ... (2 Replies)
Discussion started by: harish2015
2 Replies

3. Shell Programming and Scripting

Find and update line in xml file

Hi, I have a xml file that I need to modify 1 line to change some value from 2 to 10 (or any number). Sample input: <!-- some text here> . . . <message:test name="ryan"> <message:sample-channel charset="UTF-8" max-value="2" wait="20"> ... (5 Replies)
Discussion started by: brichigo
5 Replies

4. Shell Programming and Scripting

Find and replace last line in a file

Hi I am having a file which has like this content shown below Aaa,bb,cc,dd Xxx,yy,d,12 Dodd,12-Jun,t I need to replace last line like this Aaa,bb,cc,dd Xxx,yy,d,12 Dodd,10-August,t (13 Replies)
Discussion started by: rohit_shinez
13 Replies

5. Shell Programming and Scripting

Find the closest value in another csv file preceding it and following it?

Hi, Is this possible? I want to take a csv file and find the closest value in another csv file preceding it and following it. For ex. In this csv file, I'll take the first line: 1309341156.800000000 1309341156.802500000 1309341156.805000000 1309341156.807500000 and find the closest... (2 Replies)
Discussion started by: superbbrr
2 Replies

6. UNIX for Dummies Questions & Answers

Script to find line in one file and replace in another

Hey Guys, im looking for a script that will work under OSX. What i want to do is copy information from one file (Specific LIne) and write it to a certain line in another. To be more specific... I want the hostname of a mac to be gathered ( i assume its stored in a .plist file somewhere) and... (2 Replies)
Discussion started by: padgo
2 Replies

7. Shell Programming and Scripting

find and replace a line in a file

Hi, I am want find and replace in following content in the file. i want to repalce a word TABLESPACE XCRM_<ANY_CHAR> to TABLESPACE XCRM Sample File to Replace : LOB(COMPLEX_VALUE) STORE AS ( TABLESPACE XCRM_MED_D_NEW STORAGE(INITIAL 64K BUFFER_POOL DEFAULT) ENABLE... (3 Replies)
Discussion started by: gavemani
3 Replies

8. Shell Programming and Scripting

Mutli line pattern search & replace in a xml file

Hello guys, I need your help for a specific sed command that would search for a multi line pattern and if found, would replace it by another multi line pattern. For instance, here is the input: <RefNickName>abcd</RefNickName> <NickName>efgh</NickName> <Customize> ... (0 Replies)
Discussion started by: xciteddd
0 Replies

9. Shell Programming and Scripting

Find/Replace in XML file

I am not sure how to approach this find/replace using a shell script. Any help or guidance appreciated. I want to find this: <objects/> <thumb>thumb_0001.jpg</thumb> <preview>preview_0001.jpg</preview> And replace with something like this: <objects>... (1 Reply)
Discussion started by: jimraynor
1 Replies

10. Shell Programming and Scripting

shell call xalan to transform xml with xslt, but can not find root node

hi experts: i am using xslt to transform xml in shell. But can not find root node of source xml, if i remove the naming space definition in source xml, it works fine. So our the question is how to let xslt know the naming space of srouce xml file? Thanks for your kindly help in... (0 Replies)
Discussion started by: summer_cherry
0 Replies
Login or Register to Ask a Question