The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

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
Replace the file using sed senthil_is Shell Programming and Scripting 5 11-20-2008 07:58 AM
replace words in file based on another file kinmak Shell Programming and Scripting 9 05-07-2008 05:06 AM
Replace word in a file sasiharitha UNIX for Dummies Questions & Answers 3 12-05-2007 01:11 AM
serach and replace file name in the path in a remote xml file kiranreddy1215 Shell Programming and Scripting 1 11-12-2007 11:31 AM
Replace a Value in File lesstjm Shell Programming and Scripting 5 05-16-2007 01:20 PM

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

Join Date: Apr 2008
Posts: 57
replace nth value in xml file

Hi all,
I have application.xml file with following content

<dependency>
<groupId>fr.orange.portail.ear</groupId>
<artifactId>_AdminServicesEAR</artifactId>
<version>1.0.0-20080521.085352-1</version>
<type>ear</type>
</dependency>
<dependency>
<groupId>fr.orange.portail.ear</groupId>
<artifactId>_AdminServicesEAR</artifactId>
<version>1.0.0-20080521.085352-1</version>
<type>ear</type>
</dependency>
<dependency>
<groupId>fr.orange.portail.ear</groupId>
<artifactId>_AdminServicesEAR</artifactId>
<version>1.0.0-20080521.085352-1</version>
<type>ear</type>
</dependency>



I want to replace nth <type> tag value (ie: ear) with "OK" using sed or awk....

i tried something like this for replacing the first occurance ... but not working .

cat application.xml | awk '/\<type\>\(.*\)\<\/type\>/{n+=1}{if (n==1){gensub(/\<type\>\(.*\)\</type\>/,"OK",$0)};print }'

Can any one please tell the correct syntax of this ???

Thanks and Regards,
Subin
  #2 (permalink)  
Old 06-04-2008
Franklin52 Franklin52 is offline Forum Staff  
Moderator
  
 

Join Date: Feb 2007
Posts: 4,293
This replaces the second occurence using the variables n and s:

Code:
awk -v n=2 -v s="OK" '/<type>ear/&&n==++c{sub("ear",s)}1' file.xml
Regards
  #3 (permalink)  
Old 06-05-2008
subin_bala subin_bala is offline
Registered User
  
 

Join Date: Apr 2008
Posts: 57
Hi Franklin,

Thanks alot for your help...

How can i replace the value between the <type> </type> tag...
This will not be "ear" always... so we cannot hardcode the "ear" value...

Thanks in advane

Subin
  #4 (permalink)  
Old 06-05-2008
Franklin52 Franklin52 is offline Forum Staff  
Moderator
  
 

Join Date: Feb 2007
Posts: 4,293
You can use a variable for the types, it should be something like this:

Code:
awk -v n=2 -v s="<type>OK" -v t="<type>ear" '$0~t && n==++c{sub(t,s)}1' file
Regards
  #5 (permalink)  
Old 06-18-2008
freelong freelong is offline
Registered User
  
 

Join Date: Apr 2008
Posts: 38
Code:
awk '/<type>.*<\/type>/{n+=1; if(n==2){gsub(/<type>.*<\/type>/,"OK",$0)}}1' application.xml
Sponsored Links
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 01:10 AM.


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