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


in reply to newlines in regular expressions

The keys as it were are to note that \n\n is a blank line or if you read a file line by line then a blank line will match m/^\s*$/

perl -pi.bak -e 's/\n{2,}/\n/' file.txt
perl -pi -e 's/^\s*\z//' file.txt perl -ne 'print unless m/^\s*$/' file.txt > noblanks.txt

Update

Updated as per sgifford's comments.

cheers

tachyon