Re: sed negative lookahead (498 Views)
Reply
Occasional Advisor
JohnTal
Posts: 12
Registered: ‎06-27-2011
Message 1 of 4 (553 Views)

sed negative lookahead

Hello all,

 

I have a text file with data similar to the following:

 

<test>testdata-A&B-testdata</test>

<test>testdata-C&amp;D-testdata</test>

<test>testdata-E&F-testdata</test>

<test>testdata-G&H-testdata</test>

<test>testdata-I&amp;J-testdata</test>

 

I need to replace all instances of "&" with "&amp;".  The tricky part is that I need to replace all "&" only when NOT followed by amp;.  Basically, I need every instance of & to read &amp; in this file when I am done with it.

 

I have been trying to find a solution with sed to do this and stumbled on Negative Lookahead, which sounds promising.  I am not even sure if this is the correct path to take and I am having a hard time with the syntax. 

 

Does anyone have some advice or has encountered a similiar issue?  Any help with this issue is greatly appreciated.

 

Thank you

 

--John

Honored Contributor
Steven Schweda
Posts: 9,091
Registered: ‎02-23-2005
Message 2 of 4 (538 Views)

Re: sed negative lookahead

Acclaimed Contributor
Dennis Handly
Posts: 25,190
Registered: ‎03-06-2006
Message 3 of 4 (531 Views)

Re: sed negative lookahead

Perhaps the simplest way is to replace all "&" by "&amp;" and then replace all "&amp;amp;" by "&amp;":

sed -e 's/&/\&amp;/g' -e 's/&amp;amp;/\&amp;/g'

Occasional Advisor
JohnTal
Posts: 12
Registered: ‎06-27-2011
Message 4 of 4 (498 Views)

Re: sed negative lookahead

Thank you both for your solutions.  I tested each and they both work perfectly.

 

Steven -- In my case, there should always be at least four characters after any & because of the closing tag for that line.  So I wouldnt have to worry about a situation where the & doesn't have four characters after it.

 

Dennis -- Your solution seems so obvious, now that you brought it up.

 

Thanks again both of you.

 

--John

The opinions expressed above are the personal opinions of the authors, not of HP. By using this site, you accept the Terms of Use and Rules of Participation.