in reply to Re: replace multiple newline characters
in thread replace multiple newline characters

Provided your input-file is small enough to be read into memory

Here's one without that constraint:

perl -i.old -ne 'print if /./;' input.txt

Replies are listed 'Best First'.
Re^3: replace multiple newline characters
by haukex (Archbishop) on May 15, 2018 at 09:48 UTC

    Even shorter :-) (Update: Sorry, not quite the same, see replies.)

    $ perl -lp000e1 input.txt >output.txt
    $ perl -MO=Deparse -lp000e1 BEGIN { $/ = ""; $\ = "\n"; } LINE: while (defined($_ = readline ARGV)) { chomp $_; '???'; } continue { die "-p destination: $!\n" unless print $_; }

    Or edited:

    use warnings; use strict; local ($/,$\) = ("","\n"); while (<>) { chomp; print; }

      Shorter, yes, but it looks like you've re-introduced the "input-file is small enough to be read into memory" constraint. Or have I missed something?

        Shorter, yes, but it looks like you've re-introduced the "input-file is small enough to be read into memory" constraint. Or have I missed something?

        Hm, good point, I guess in my excitement over the nice short oneliner I sort of did. -000* turns on paragraph mode, so if the file contains lots of short "paragraphs" with blank lines in between, it'll be fine, but if the file contains, say, a block of several MB of non-blank lines, a single blank line, and another huge paragraph, then yes, you're right, my solution isn't that great.

        * perlrun is a little unclear whether it's -00 or -000, both seem to work.