http://www.perlmonks.org?node_id=921824


in reply to Re^2: Removing double carriage return
in thread Removing double carriage return

From the command line, this replaces 3 or more newlines with 2 newlines or replaces exactly 2 newlines with 1 newline.

perl -i.bak -0777 -pe 's/(\n{3,}|\n\n)/2 == length $1 ? "\n" : "\n\n"/eg' inputfile

Notice that this looks for the longest match first (so that 2 newlines won't match more than 2, i.e. 3 or more).

Update: That could be simplified to:

perl -i.bak -0777 -pe 's/\n+/2 < length $& ? "\n\n" : "\n"/ge' inputfile