# AWK String replacement

**URL:** <https://community.unix.com/t/awk-string-replacement/204880>\
**Category:** Shell Programming and Scripting\
**Created:** [July 15, 2008, 3:22am UTC](https://community.unix.com/t/awk-string-replacement/204880 "2008-07-15T03:22:36Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![subin\_bala](https://community.unix.com/letter_avatar/subin_bala/32/6_449c533f586939b6e5f246632a46421f.png) [@subin\_bala](https://community.unix.com/u/subin_bala)\
**Post date:** [July 15, 2008, 3:22am UTC](https://community.unix.com/t/awk-string-replacement/204880/1 "2008-07-15T03:22:36Z")

</div>

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:confused:

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?:rolleyes:

---

<div class="post-metadata">

**Author:** ![zaxxon](https://community.unix.com/user_avatar/community.unix.com/zaxxon/32/660_2.png) [@zaxxon](https://community.unix.com/u/zaxxon)\
**Post date:** [July 15, 2008, 4:35am UTC](https://community.unix.com/t/awk-string-replacement/204880/2 "2008-07-15T04:35:33Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![subin\_bala](https://community.unix.com/letter_avatar/subin_bala/32/6_449c533f586939b6e5f246632a46421f.png) [@subin\_bala](https://community.unix.com/u/subin_bala)\
**Post date:** [July 15, 2008, 4:52am UTC](https://community.unix.com/t/awk-string-replacement/204880/3 "2008-07-15T04:52:39Z")

</div>

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?😛

---

<div class="post-metadata">

**Author:** ![zaxxon](https://community.unix.com/user_avatar/community.unix.com/zaxxon/32/660_2.png) [@zaxxon](https://community.unix.com/u/zaxxon)\
**Post date:** [July 15, 2008, 5:10am UTC](https://community.unix.com/t/awk-string-replacement/204880/4 "2008-07-15T05:10:34Z")

</div>

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

```

---

<div class="post-metadata">

**Author:** ![subin\_bala](https://community.unix.com/letter_avatar/subin_bala/32/6_449c533f586939b6e5f246632a46421f.png) [@subin\_bala](https://community.unix.com/u/subin_bala)\
**Post date:** [July 16, 2008, 2:18am UTC](https://community.unix.com/t/awk-string-replacement/204880/5 "2008-07-16T02:18:01Z")

</div>

:b: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" ?

---

<div class="post-metadata">

**Author:** ![zaxxon](https://community.unix.com/user_avatar/community.unix.com/zaxxon/32/660_2.png) [@zaxxon](https://community.unix.com/u/zaxxon)\
**Post date:** [July 16, 2008, 2:29am UTC](https://community.unix.com/t/awk-string-replacement/204880/6 "2008-07-16T02:29:58Z")

</div>

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