If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > Unix Shell Scripts > Replacing a pattern based on the occurence of another pattern

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 09-14-04, 12:05
cssriram cssriram is offline
Registered User
 
Join Date: Sep 2004
Posts: 3
Question Replacing a pattern based on the occurence of another pattern

Hi,

I want to do the following

Assume there is an xml which contains entries like the following...

<a attr1="value1" attr2="value2"/>
<b attr1="value3" attr2="value4" attr3="value5"/>
<a attr1="value1" attr2="value2"/>
....


I want to replace the "attr1" to say "attr3" in every occurence of <a/> tag.

I need the result to be something like

<a attr3="value1" attr2="value2"/>
<b attr1="value3" attr2="value4" attr3="value5"/>
<a attr3="value1" attr2="value2"/>
....

Is there any tool to do the above?
Any shell scripts?

Please reply...

Thanks and Regards,

Sriram.C.S
Reply With Quote
  #2 (permalink)  
Old 09-15-04, 04:44
ducasio ducasio is offline
Registered User
 
Join Date: Aug 2004
Location: Rome, Italy
Posts: 81
you can use sed on command line or inside of a script this way:

sed -e '/<a attr1/s/attr1/attr3/' file.xml > file.temp
mv file.temp file.xml
Reply With Quote
  #3 (permalink)  
Old 09-19-04, 00:31
cssriram cssriram is offline
Registered User
 
Join Date: Sep 2004
Posts: 3
Question

Hi ducasio,

My example was simple enough. Actually I wanted something like this

<a attr1="value1" attr2="value2"/>
<b attr2="value4" attr1="value3" attr5="value5"/>
<a attr2="value2" attr1="value1"/>
<a attr4="valueN" attr2="value2" .....attr1="valueX"... />

The attr1 need not be placed near start tag <a>. Hence to search by pattern that attr1 is always adjacent to <a> should not be there.

In such a case how do we go about doing it?

Please reply.

Sriram.C.S
Reply With Quote
  #4 (permalink)  
Old 09-20-04, 06:38
iaguigon iaguigon is offline
Registered User
 
Join Date: May 2004
Location: Barcelona, Spain
Posts: 54
Sriram,

You have to look for /<a/ patterns and then put attr3 instead of attr1.
It seems pretty straightforward to reach there from ducasio's script.

Regards

iaguigon
Reply With Quote
  #5 (permalink)  
Old 09-20-04, 12:28
cssriram cssriram is offline
Registered User
 
Join Date: Sep 2004
Posts: 3
Hi,

Not sure why I missed a straight forward thing.

Thanks...ducasio and iaguigon

Regards,

Sriram.C.S
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On