$file_data =~ s/\n\n+/\n/gs;
Should work. On an unrelated note:
# Suggest using scalar filehandles and checking to # see if open succeeded... open( my $my_filehandle, $file ) || next; # possibly: || die "Can't open file: $!"; local $/ = undef; # tell perl not to stop reading at newline my $file_data = <$my_filehandle>; close $my_filehandle; # process $file_data # etc...
The 'open' command is very prone to failure on UNIX systems for lots of reasons (you don't have access to the file, it's not readable, the file's really a directory, etc...), so getting in the habit of checking it is a plus.

If you use scalar filehandles, you can use them with 'my' to keep them local to your block.

local $/ = undef; tells perl that newlines shouldn't be considered the 'end of input'. If you want to look at the whole file instead of individual lines, you can turn it off inside the block and the next read on the filehandle will give you the whole thing.

The 's' option after the substitution regex tells perl to not treat newlines as the end of the string in a regular expression. Then you can treat them as normal characters and remove them when there are a few in a row.

If lines have spaces on them (and you want to remove those 'empty' lines too), then:
$file_data =~ s/\n[\s\n]+/\n/gs;
should work.

You can do this task from the command line:
$ perl -ni -e 'print if /\S/' *.shtml
Although that's technically a line-by-line approach.
Update: Fixed typo ($/ not $\);
Update 2:Arr! Fixed problem #2 (parens around open, cause I don't like to use 'or' instead of '||');

In reply to Re: Removing white-lines... by saintly
in thread Removing white-lines... by pyro.699

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.