[ILUG] Re: sed question

Marcus Furlong furlongm at hotmail.com
Thu Aug 14 16:13:09 IST 2008


Marcus Furlong <furlongm <at> hotmail.com> writes:

> 
> Hi,
> 
> I'm having trouble using sed to do replacements on some badly tagged
> xml. I have a large number of files that are tagged as follows:

Ok this problem is fixed now, but it introduced another smaller one...

So I have other tags as follows:

<third> lots of text
<third> lots of text
<third> lots of text
<third> lots of text
<third> lots of text

Modifying the previous perl example I tried to add the closing tags as follows:

perl -pi -e 'undef $/; s{(<third.*?)(<third)}{$1</third>\n$2}sg'

The problem is that this only works for every other "third" tag, because the
opening tag becomes part of the regexp matched pattern and isn't processed in
the following match. E.g it becomes:

<third> lots of text</third>
<third> lots of text
<third> lots of text</third>
<third> lots of text
<third> lots of text</third>

Running the script twice doesn't help either as it matches those that are
already done, and again skips the following one:

<third> lots of text</third></third>
<third> lots of text
<third> lots of text</third></third>
<third> lots of text
<third> lots of text</third></third>

Any ideas on how to this one? Thanks,

Marcus





More information about the ILUG mailing list