Modify XML tag using shell script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Modify XML tag using shell script
# 1  
Old 06-05-2013
Modify XML tag using shell script

Hi All

Need some help with a unix shell script. I have a XML file as shown below:
Code:
<Root>
<Service>
<endPoint type="SOAP" protocol="http">
<provider>ABCD</provider>
<urlRewrite>/service/xyz/getAccountDetails</urlRewrite>
<timeout>30</timeout>
</endPoint>
</Service>
<Service>
<endPoint type="SOAP" protocol="http">
<provider>EFGH</provider>
<urlRewrite>/service/efg/deleteCustomerAccount</urlRewrite>
<timeout>30</timeout>
</endPoint>
</Service>
</Root>

My requirement is to look up for each occurence of the XML node "urlRewrite" in the above file, tokenize the value of this "urlRewrite" node based on '/' and then replace the value with "/test/${lastToken}". All other nodes should be copied as is to the output file.

So the output file should look like below:
Code:
<Root>
<Service>
<endPoint type="SOAP" protocol="http">
<provider>ABCD</provider>
<urlRewrite>/test/getAccountDetails</urlRewrite>
<timeout>30</timeout>
</endPoint>
</Service>
<Service>
<endPoint type="SOAP" protocol="http">
<provider>EFGH</provider> 
<urlRewrite/test/deleteCustomerAccount</urlRewrite>
<timeout>30</timeout>
</endPoint>
</Service>
</Root>

Any help on this is much appreciated

Thanks
Ankur

Last edited by Franklin52; 06-05-2013 at 07:53 AM.. Reason: Please use code tags
# 2  
Old 06-05-2013
How about this ?
Code:
 
perl -pe 's/(\<urlRewrite\>)(.*)\/(.+?)(\<\/urlRewrite\>)/$1\/test\/$3$4/' filename

# 3  
Old 06-05-2013
Thnaks a ton Pravin. Works perfectly. Just one question. I am not much of a Unix scripting guy so need to know if this would work even if the "urlRewrite tag has more than 3 tokens. I mean the value could have n number of '/'. The code should pick the last token and replace it with "/test/${lastToken}".

For example if the value under "urlRewrite is as below:

<urlRewrite>/service/abc/def/xyz/getAccountDetails</urlRewrite>

Will your code still replace it with :

<urlRewrite>/test/getAccountDetails</urlRewrite>

Thanks Again for your prompt response.
# 4  
Old 06-05-2013
below is sample file, which i have modified (original from post #1)
You can run perl code on this sample file and check result.
Code:
<Root>
<Service>
<endPoint type="SOAP" protocol="http">
<provider>ABCD</provider>
<urlRewrite>/service/xyz/getAccountDetails</urlRewrite>
<timeout>30</timeout>
</endPoint>
</Service>
<Service>
<endPoint type="SOAP" protocol="http">
<provider>EFGH</provider>
<urlRewrite>/service/efg/deleteCustomerAccount</urlRewrite>
<urlRewrite>/service/abc/def/xyz/getAccountDetails</urlRewrite>
<urlRewrite>/service/abc/def/xyz/getAccountDetails</urlRewrite>
<urlRewrite>/service/abc/def/xyz//test5/test8/test9/getAccountDetails</urlRewrite>
<urlRewrite>/service//test1/test2/test3/abc/def/xyz/getAccountDetails</urlRewrite>
<timeout>30</timeout>
</endPoint>
</Service>
</Root>

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Moving XML tag/contents after specific XML tag within same file

Hi Forum. I have an XML file with the following requirement to move the <AdditionalAccountHolders> tag and its content right after the <accountHolderName> tag within the same file but I'm not sure how to accomplish this through a Unix script. Any feedback will be greatly appreciated. ... (19 Replies)
Discussion started by: pchang
19 Replies

2. Shell Programming and Scripting

Read xml tags and then remove the tag using shell script

<Start> <Header> This is header section </Header> <Body> <Body_start> This is body section <a> <b> <c> <st>111</st> </c> <d> <st>blank</st> </d> </b> </a> </Body_start> <Body_section> This is body section (3 Replies)
Discussion started by: RJG
3 Replies

3. Shell Programming and Scripting

To search for a particular tag in xml and collate all similar tag values and display them count

I want to basically do the below thing. Suppose there is a tag called object1. I want to display an output for all similar tag values under heading of Object 1 and the count of the xmls. Please help File: <xml><object1>house</object1><object2>child</object2>... (9 Replies)
Discussion started by: srkmish
9 Replies

4. Shell Programming and Scripting

Print a closing XML tag shell script

I have a shell script that does everything I need it to do. But, when I was testing it I realized it doesn't print the closing XML tag.... Does anyone know how to incorporate printing the XML tag with my script? I am using AWK any help would be appreciated. (4 Replies)
Discussion started by: risarose87
4 Replies

5. Shell Programming and Scripting

Using shell command need to parse multiple nested tag value of a XML file

I have this XML file - <gp> <mms>1110012</mms> <tg>988</tg> <mm>LongTime</mm> <lv> <lkid>StartEle=ONE, Desti = Motion</lkid> <kk>12</kk> </lv> <lv> <lkid>StartEle=ONE, Source = Velocity</lkid> <kk>2</kk> </lv> <lv> ... (3 Replies)
Discussion started by: NeedASolution
3 Replies

6. Shell Programming and Scripting

How can I remove some xml tag lines using shell script?

Hi All, My name is Prathyu and I am working as a ETL develper. I have one requirement to create a XML file based on the provided XSD file. As per the Datastage standards Key(repeatable) field does not contain any Null values so I am inserting some dummy tag line to that XML file. ... (14 Replies)
Discussion started by: Prathyu
14 Replies

7. UNIX for Advanced & Expert Users

Shell Script to read XML tags and the data within that tag

Hi unix Gurus, I am really new to Unix Scripting. Please help me to create a shell script which reads the xml file and from that i need to fetch a particular information. For example <SOURCE BUSINESSNAME ="" DATABASETYPE ="Teradata" DBDNAME ="DWPROD3" DESCRIPTION ="" NAME... (2 Replies)
Discussion started by: SmilePlease
2 Replies

8. Shell Programming and Scripting

shell command to remove some XML tag is needed

Hi all, I have a file which i have to remove some line from it, the lines that i have to remove from my file is as below: </new_name></w"s" langue="Fr-fr" version="1.0" encoding="UTF-8" ?> <New_name> and it is finding at the middle of my file, is there any command line in linux to do it or do... (10 Replies)
Discussion started by: id_2pc
10 Replies

9. Shell Programming and Scripting

How to remove some xml tag lines using shell script

I have existing XML file as below, now based on input string in shell script on workordercode i need to create a seprate xml file for e.g if we pass the input string as 184851 then it find the tag data from <workOrder>..</workOrder> and write to a new file and similarly next time if i pass the... (3 Replies)
Discussion started by: balrajg
3 Replies

10. Shell Programming and Scripting

Searching XML tag in a script

Hey, I need to parse the following XML to just grab the Customer ID. Is there any RegEx that can achieve this ? So in this example, the script just return 0000109654, as the output. Even if it involves awk, sed please let me know. ************ <?xml version="1.0" encoding="UTF-8"?>... (1 Reply)
Discussion started by: abhandari
1 Replies
Login or Register to Ask a Question