perl -pi -e 's!^\s+?$!!' file.txt or s!^(\n|\s+)$!!g