# Modify XML tag using shell script

**URL:** <https://community.unix.com/t/modify-xml-tag-using-shell-script/330098>\
**Category:** Shell Programming and Scripting\
**Created:** [June 5, 2013, 6:39am UTC](https://community.unix.com/t/modify-xml-tag-using-shell-script/330098 "2013-06-05T06:39:36Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![abhilwa](https://community.unix.com/letter_avatar/abhilwa/32/6_449c533f586939b6e5f246632a46421f.png) [@abhilwa](https://community.unix.com/u/abhilwa)\
**Post date:** [June 5, 2013, 6:39am UTC](https://community.unix.com/t/modify-xml-tag-using-shell-script/330098/1 "2013-06-05T06:39:36Z")

</div>

Hi All

Need some help with a unix shell script. I have a XML file as shown below:

```nohighlight
<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:

```nohighlight
<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

---

<div class="post-metadata">

**Author:** ![pravin27](https://community.unix.com/letter_avatar/pravin27/32/6_449c533f586939b6e5f246632a46421f.png) [@pravin27](https://community.unix.com/u/pravin27)\
**Post date:** [June 5, 2013, 6:44am UTC](https://community.unix.com/t/modify-xml-tag-using-shell-script/330098/2 "2013-06-05T06:44:39Z")

</div>

How about this ?

```nohighlight
 
perl -pe 's/(\<urlRewrite\>)(.*)\/(.+?)(\<\/urlRewrite\>)/$1\/test\/$3$4/' filename

```

---

<div class="post-metadata">

**Author:** ![abhilwa](https://community.unix.com/letter_avatar/abhilwa/32/6_449c533f586939b6e5f246632a46421f.png) [@abhilwa](https://community.unix.com/u/abhilwa)\
**Post date:** [June 5, 2013, 6:57am UTC](https://community.unix.com/t/modify-xml-tag-using-shell-script/330098/3 "2013-06-05T06:57:48Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![pravin27](https://community.unix.com/letter_avatar/pravin27/32/6_449c533f586939b6e5f246632a46421f.png) [@pravin27](https://community.unix.com/u/pravin27)\
**Post date:** [June 5, 2013, 7:25am UTC](https://community.unix.com/t/modify-xml-tag-using-shell-script/330098/4 "2013-06-05T07:25:09Z")

</div>

below is sample file, which i have modified (original from post #1)  
You can run perl code on this sample file and check result.

```nohighlight
<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>

```
