Hi Monks,

I have an HTML text file in this kind of format:

<html> ...etc... <pre> Line 1 Line 2 ...etc... Line n </pre> ...etc... </html>

I'd like to put a <p> tag at the end of each line which appears between the <pre> & </pre> tags, and finally remove the <pre> & </pre> tags, to give me this:

<html> ...etc... Line 1<p> Line 2<p> ...etc...<p> Line n<p> ...etc... </html>

I guess the simple and efficient way to do that would be to process the file line-by-line (and I might end up using such code), but first I'd like to see how it could be done treating the whole file as a single line. Here's the code I tried:

perl -0 -pe '1 while (s/(<pre>\n.*?)\n(.*<\/pre>)/$1<p>$2/ms);s/<\/?pr +e>//g' htmlfile
That works, except it strips the \n chars out like this of course:
<html> ...etc... <pre> Line 1<p>Line 2<p>...etc...<p>Line n<p></pre> ...etc... </html>

So I tried this:

perl -0 -pe '1 while (s/(<pre>\n.*?)\n(.*<\/pre>)/$1<p>\n$2/ms);s/<\/? +pre>//g' htmlfile
But that loops infintely because it keeps on matching "Line 1", which becomes "Line 1<p><p><p>...".

How can I concisely write such code, processing htmlfile as a single line?

Thanks...Terry


In reply to Substitution inside tags, as 1 line by tel2

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.