replace nth value in xml file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting replace nth value in xml file
# 1  
Old 06-04-2008
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 . Smilie

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  
Old 06-04-2008
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  
Old 06-05-2008
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  
Old 06-05-2008
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  
Old 06-18-2008
Code:
awk '/<type>.*<\/type>/{n+=1; if(n==2){gsub(/<type>.*<\/type>/,"OK",$0)}}1' application.xml

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Replace a string in a xml file

Hello, I have below xml file, I want to find line default-value and replace the string within quotes followed by default-value "moni/Websphere/". Replace moni/Websphere/ with monitor/AMQ/ <monitor> <name>WebsphereMqMonitor</name> <type>managed</type> <argument... (4 Replies)
Discussion started by: prince1987
4 Replies

2. Shell Programming and Scripting

How to search and replace string from nth column from a file?

I wanted to search for a string and replace it with other string from nth column of a file which is comma seperated which I am able to do with below # For Comma seperated file without quotes awk 'BEGIN{OFS=FS=","}$"'"$ColumnNo"'"=="'"$PPK"'"{$"'"$ColumnNo"'"="'"$NPK"'"}{print}' ${FileName} ... (5 Replies)
Discussion started by: Amit Joshi
5 Replies

3. Shell Programming and Scripting

sed : replace Nth match in a file

I have a situation where a file "config.txt" looks like this Servername: OS: Serername: OS: Servername: OS: .... .... ... Servername: OS: looking for the sed syntax to replace the "Nth" occurrence of Servername (i would apply the same logic to OS as well), want to replace the Nth... (4 Replies)
Discussion started by: alldbest
4 Replies

4. Shell Programming and Scripting

Replace pattern from nth field from a file

I have posted this again as old post is closed and I am not able to reopen. so please consider this new post Input File : 1,A,Completed,06.02_19.36,Jun 30 20:00 2,BBB,Failed,07.04_05.12,Jul 21 19:06 3,CCCCC,New,07.21_03.03,Jul 26 12:57 4,DDDDD,Pending,, I wast output file as: ... (7 Replies)
Discussion started by: Amit Joshi
7 Replies

5. Shell Programming and Scripting

Replace pattern from nth field from a file

$ cat /cygdrive/d/Final2.txt 1,A ,Completed, 07.03_23.01 ,Jun 30 20:00 2,BBB,Pending,, 3,CCCCC,Pending,, 4,DDDDD,Pending,, 5,E,Pending,, 6,FFFF,Pending,, 7,G,Pending,, In the above file 4th field is date which is in MM.DD_HH.MIN format and I need to convert it to as it is there in 5th... (1 Reply)
Discussion started by: Amit Joshi
1 Replies

6. Shell Programming and Scripting

Replace nth to nth character?

Hi I got the following problem and I wonder if some could please help me out? I'd like to replace character 8 - 16 , 16 - 24 cat file ... (2 Replies)
Discussion started by: stinkefisch
2 Replies

7. Shell Programming and Scripting

Replace a value of Nth field of nth row

Using Awk, how can I achieve the following? I have set of record numbers, for which, I have to replace the nth field with some values, say spaces. Eg: Set of Records : 4,9,10,55,89,etc I have to change the 8th field of all the above set of records to spaces (10 spaces). Its a delimited... (1 Reply)
Discussion started by: deepakwins
1 Replies

8. Emergency UNIX and Linux Support

Replace nth position character of all the lines in file

I want to replace 150th character of all the lines in a file using sed or awk... searched the forums but didn't find exact answer (9 Replies)
Discussion started by: greenworld123
9 Replies

9. UNIX for Dummies Questions & Answers

replace %20 in xml file

Hi, I'd like to use sed in order to replace %20 and other "special" characters that are represented with % and some number combination in xml file. Typical line looks like this: /Users/imac1/Music/iTunes/iTunes... (6 Replies)
Discussion started by: andrejm
6 Replies

10. UNIX for Dummies Questions & Answers

Replace nth character in a file with a period

Hi all, If you look at the example below,I want to replace the 21st character (,) with a period (.). I have 1000 records in a file can someone help me how to do that. Thankyou all in advance. "2008-07-15... (3 Replies)
Discussion started by: blackhawk_123
3 Replies
Login or Register to Ask a Question