in reply to why does 'sed' remove the entire line?

What if you just used Perl? as long as you're in there, no reason at all to fork to sed: $line =~ s/\s+$//mg....