I like this regular expression which is platform-independent (well, as far as Windows, Linux, and Mac systems are concerned):
my $lineend = qr/\n\r|\r\n|\n|\r/; $data =~ s/($lineend){3}/$1$1/sg;

This defines a regular expression which detects the end of a line. It first checked for a newline - carriage return combination. If this is not found it checks for a carriage return - newline combination. If neither of these two combinations are found, then it checks for a newline or a carriage return.

The next expression assumes you have your entire file in $data. It then replaces 3 end-of-line combinations (2 blank lines) with 2 of them (2 end-of-line combinations will produce a single blank line).

Actually, if the blank lines might contain whitespace, then you could consider the following expression instead:

$data =~ s/($lineend)\s*($lineend)\s*($lineend)/$1$1/sg;

(Regexps tested ok on linux).


In reply to Re: Double blank lines to single blank line by monarch
in thread Double blank lines to single blank line by proactive1

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.