replace & with & xml file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting replace & with & xml file
# 1  
Old 05-27-2010
replace & with & xml file

Hello All
I have a xml file with many sets of records
like this
Code:
<mytag>mydata</mytag>
<tag2>data&</tag2>

also same file can be like this
Code:
<mytag>mydata</mytag>
<tag2>data&</tag2>
<tag3>data2&amp;data3</tag3>

Now i can grep & and replace with &amp; for whole file but it will replace all occurances whihc is not desirable.
i need output like
Code:
<mytag>mydata</mytag>
<tag2>data&amp;</tag2>
<tag3>data2&amp;data3</tag3>

any suggestions/idea will be of great help.
# 2  
Old 05-27-2010
Which occurrences don't you want to change? We need to know so as to skip them.
# 3  
Old 05-27-2010
as i mentioned in out put i need to remove & which are not with &amp;
Need output like
intial file
Code:
<mytag>mydata</mytag>
<tag2>data</tag2>
<tag3>data2&amp;data3</tag3>

output
Code:
<mytag>mydata</mytag>
<tag2>data&amp;</tag2>
<tag3>data2&amp;data3</tag3>

# 4  
Old 05-27-2010
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 -

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

tyler_durden
This User Gave Thanks to durden_tyler For This Post:
# 5  
Old 05-27-2010
wow that works just fine...
Thanks a lot buddy!
Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

I want replace string <enclosure>&#x22;</enclosure> with <enclosure>&#x5e;</enclosure> in xml

I have xml files with with extension .ktr in subfolders i want to replace the string <enclosure>&#x22;</enclosure> with <enclosure>&#x5e;</enclosure> i have written logic but it is not working correctly sed -i '' 's#<enclosure>&\#x22;</enclosure>#<enclosure>&\#x5e;</enclosure>#g' *.ktr ... (3 Replies)
Discussion started by: reddy12
3 Replies

2. Shell Programming and Scripting

SFTP Shell Script Get & Delete && Upload & Delete

Hi All, Do you have any sample script, - auto get file from SFTP remote server and delete file in remove server after downloaded. - only download specify filename - auto upload file from local to SFTP remote server and delete local folder file after uploaded - only upload specify filename ... (3 Replies)
Discussion started by: weesiong
3 Replies

3. Shell Programming and Scripting

Replace dashes positions 351-357 & 024-043 with 0 & replace " " if exis with 04 at position 381-382

I need to replace dashes (i.e. -) if present from positions 351-357 with zero (i.e. 0), I also need to replace dash (i.e “-“) if present between position 024-043 with zero (i.e. 0) & I replace " " (i.e. 2 space characters) if present at position 381-382 with "04". Total length of record is 413.... (11 Replies)
Discussion started by: lancesunny
11 Replies

4. Shell Programming and Scripting

Mutli line pattern search & replace in a xml file

Hello guys, I need your help for a specific sed command that would search for a multi line pattern and if found, would replace it by another multi line pattern. For instance, here is the input: <RefNickName>abcd</RefNickName> <NickName>efgh</NickName> <Customize> ... (0 Replies)
Discussion started by: xciteddd
0 Replies

5. Shell Programming and Scripting

Sort a the file & refine data column & row format

cat file1.txt field1 "user1": field2:"data-cde" field3:"data-pqr" field4:"data-mno" field1 "user1": field2:"data-dcb" field3:"data-mxz" field4:"data-zul" field1 "user2": field2:"data-cqz" field3:"data-xoq" field4:"data-pos" Now i need to have the date like below. i have just... (7 Replies)
Discussion started by: ckaramsetty
7 Replies

6. Shell Programming and Scripting

Replace & sign to &amp word

Hi, I have text file abc.txt. In this file, I have the following data. Input: Mr Smith &amp Mrs Smith Mr Smith &apos Mrs Smith Mr Smith & Mrs Smith Mr Smith& Mrs Smith Mr Smith &Mrs Smith Output: Mr Smith &amp Mrs Smith Mr Smith &apos Mrs Smith Mr Smith &amp Mrs Smith Mr Smith&amp... (4 Replies)
Discussion started by: naveed
4 Replies

7. UNIX for Dummies Questions & Answers

How to compare 2 files & get specific value & replace it in other file.

Hiiii Friends I have 2 files with huge data. I want to compare this 2 files & if they hav same set of vales in specific rows & columns i need to get that value from one file & replace it in other. For example: I have few set data of both files here: a.dat: PDE-W 2009 12 16 5 29 11.11 ... (10 Replies)
Discussion started by: reva
10 Replies

8. Shell Programming and Scripting

PHP read large string & split in multidimensional arrays & assign fieldnames & write into MYSQL

Hi, I hope the title does not scare people to look into this thread but it describes roughly what I'm trying to do. I need a solution in PHP. I'm a programming beginner, so it might be that the approach to solve this, might be easier to solve with an other approach of someone else, so if you... (0 Replies)
Discussion started by: lowmaster
0 Replies

9. Shell Programming and Scripting

Find & Replace string in multiple files & folders using perl

find . -type f -name "*.sql" -print|xargs perl -i -pe 's/pattern/replaced/g' this is simple logic to find and replace in multiple files & folders Hope this helps. Thanks Zaheer (0 Replies)
Discussion started by: Zaheer.mic
0 Replies

10. Shell Programming and Scripting

XML Copy & replace problem

I probably could have done this at one time, but, the years and no need has left my scripting skills lacking and I'm unable to work this problem out. https://www.unix.com/images/smilies/frown.gif :( Using Linux, have a great many xml files in which there may be multiple occurrence of a line of... (13 Replies)
Discussion started by: xenixuser
13 Replies
Login or Register to Ask a Question