replace & with & xml file

Hello All
I have a xml file with many sets of records
like this

<mytag>mydata</mytag>
<tag2>data&</tag2>

also same file can be like this

<mytag>mydata</mytag>
<tag2>data&</tag2>
<tag3>data2&data3</tag3>

Now i can grep & and replace with & for whole file but it will replace all occurances whihc is not desirable.
i need output like

<mytag>mydata</mytag>
<tag2>data&</tag2>
<tag3>data2&data3</tag3>

any suggestions/idea will be of great help.

Which occurrences don't you want to change? We need to know so as to skip them.

as i mentioned in out put i need to remove & which are not with &
Need output like
intial file

<mytag>mydata</mytag>
<tag2>data</tag2>
<tag3>data2&data3</tag3>

output

<mytag>mydata</mytag>
<tag2>data&</tag2>
<tag3>data2&data3</tag3>

I guess you could use negative lookahead to solve this. I've added some dummy (although improbable) data in the input file to test different cases -

$
$
$ cat f2
<mytag>mydata</mytag>
<tag2>data&</tag2>
<tag3>data2&data3</tag3>
<tag4>data& </tag4>
<tag5> & </tag5>
<tag6> & </tag6>
<tag7>&&&</tag7>
<tag8> & & </tag8>
$
$
$ perl -plne 's/&(?!amp;)/&/g' f2
<mytag>mydata</mytag>
<tag2>data&</tag2>
<tag3>data2&data3</tag3>
<tag4>data& </tag4>
<tag5> & </tag5>
<tag6> & </tag6>
<tag7>&&&</tag7>
<tag8> & & </tag8>
$
$

tyler_durden

1 Like

wow that works just fine...
Thanks a lot buddy!
:slight_smile: