in reply to replacing blank lines

The problem is you're reading the file one line at a time. If you find a blank line, you're essentially replacing it with another blank line, so there's no net change. If you process it one line at a time, you need to write nonblank lines out to a separate file as you go, so it will have the nonblanks when you're done. If you know the file won't be too big, I think you could also use this regex:
$entire_file =~ s/^\n$//mg;
That should leave $entire_file with only nonblank lines. HTH.

Replies are listed 'Best First'.
Re: Re: replacing blank lines
by KM (Priest) on Mar 06, 2001 at 05:03 UTC
    $entire_file =~ s/^\n$//mg;

    That should leave $entire_file with only nonblank lines

    This is assuming that blank lines are TRUELY blank (no spaces). This is more general:

    perl -pi -e 's!^\s+?$!!' file.txt or s!^(\n|\s+)$!!g

    Cheers,
    KM

      The first one-liner will not have the intended effect:     perl -pi -e 's,^\s+?$,,' file.txt only works on lines that consist of a newline. For lines that contain whitespace followed by a newline, the newline is not removed because of the non-greedy quantifier. (Although you could run the one-liner twice... ;)

      In the second one-liner:     perl -pi -e 's,^(\n|\s+)$,,g' file.txt the alternation and the /g are unnecessary.

      This is sufficient: perl -pi -e 's,^\s+$,,' file.txt