in reply to replace multiple newline characters

Provided your input-file is small enough to be read into memory, you can do this with a one-liner:
perl -i.old -0777 -pe 's/\n+/\n/sg' input.txt
the original version of the file is kept as input.txt.old.

Replies are listed 'Best First'.
Re^2: replace multiple newline characters
by hippo (Archbishop) on May 15, 2018 at 08:15 UTC
    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

      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?