The hint -0 is good, since you can use -0777 to delete the whole file in -p mode. Read more about these guys in perlrun. So, this oneliner should do the trick:
$ perl -0777 -pe 's/\n{5,}/\n\n/g'
If there are up to four lines in a row, nothing happens. Five new lines or more (four empty lines or more) are replaced by two new lines (one empty line). Pay attention to the /g switch here to replace more than just the first match.
Delayed code:
BEGIN { $/ = undef; $\ = undef; } LINE: while (defined($_ = <ARGV>)) { s/\n{5,}/\n\n/g; } continue { die "-p destination: $!\n" unless print $_; }
NTN! :)
source share