The UNIX and Linux Forums  


Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
string replacement in a sequence of characters raoscb UNIX for Dummies Questions & Answers 3 07-08-2008 07:00 AM
String Replacement Script jbud Shell Programming and Scripting 6 11-15-2007 06:37 AM
String Replacement with Perl Lindarella Shell Programming and Scripting 4 09-29-2006 03:05 PM
sed problem - replacement string should be same length as matching string. amangeles Shell Programming and Scripting 4 01-11-2006 06:11 AM
String replacement in multiple files WABonnett Shell Programming and Scripting 2 02-17-2004 03:49 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 07-15-2008
subin_bala subin_bala is offline
Registered User
  
 

Join Date: Apr 2008
Posts: 57
AWK String replacement

I have an xml file with following tags
<NewTag>value123</xyz>
<NewTag>value321</abcd>

I have to replace the values in between the tags with some value ( VAL1/VAL2)
but the thing the ending tag can be any thing, for this i need a awk command

currently i am using this but it doesn't work

awk -v n=$OCCURANCE -v s="<$TAGNAME>$TAGVALUE</$TAGNAME>" "/<$TAGNAME(.+)>/&&n==++c{sub(\"<$TAGNAME>(.+)>\",s)}1" $FILENAME

the problem with this expression is the string s which i am building is a static one it doesn't take care of the changing ending tags!!

can some one suggest me the proper expression?
  #2 (permalink)  
Old 07-15-2008
zaxxon's Avatar
zaxxon zaxxon is offline Forum Staff  
Moderator
  
 

Join Date: Sep 2007
Location: Germany
Posts: 2,289
I tried to understand it but I didn't. Can you post a sample output of what you want to have? And why can the ending Tag be another than the opening Tag?
  #3 (permalink)  
Old 07-15-2008
subin_bala subin_bala is offline
Registered User
  
 

Join Date: Apr 2008
Posts: 57
what i exactly want to do is this

the xml file i need to change contains followin tags

<Myproperty Name="Colour">RED</Myproperty>
<MyName NickName="ppp">MYName</MyName>


you can see that the starting tag and ending tag are different!
i need to get the following out put when i execute the command

<Myproperty Name="Colour">GREEN</Myproperty>
<MyName NickName="ppp">URName</MyName>

hope it's clear?
  #4 (permalink)  
Old 07-15-2008
zaxxon's Avatar
zaxxon zaxxon is offline Forum Staff  
Moderator
  
 

Join Date: Sep 2007
Location: Germany
Posts: 2,289

Code:
cat infile

<Myproperty Name="Colour">RED</Myproperty>
<MyName NickName="ppp">MYName</MyName>

sed -e '/"Colour"/ { s/>RED</>GREEN</g }' -e '/"ppp"/ { s/MYName/URName/g }' infile

<Myproperty Name="Colour">GREEN</Myproperty>
<MyName NickName="ppp">URName</MyName>

  #5 (permalink)  
Old 07-16-2008
subin_bala subin_bala is offline
Registered User
  
 

Join Date: Apr 2008
Posts: 57
Thanks a lot zaxxon!

can you tell me how can i replace only a speific occurance of this pattern! i mean say i want o replace the 3rd occurance of the RED, then what will be flag for the sed command instead of "g" ?
  #6 (permalink)  
Old 07-16-2008
zaxxon's Avatar
zaxxon zaxxon is offline Forum Staff  
Moderator
  
 

Join Date: Sep 2007
Location: Germany
Posts: 2,289

Code:
sed -e '/"Colour"/ { s/>RED</>GREEN</3 }' -e '/"ppp"/ { s/MYName/URName/3 }' infile

But remember that it will just be the 3rd of the line, not of the whole input as one.
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 04:29 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0