A regular expression like s/^[^\S\n]*\n//gm
will remove all lines that contain only whitespace.
Update
If you want to do something more than just remove blank lines, then it is best to split
the string into lines and remove the ones you don't want. This code uses grep
to remove lines that are all-whitespace and then pop
to remove the last one.
my @lines = grep /\S/, split /\n/, $var;
pop @lines;
$var = join '', map "$_\n", @lines;
print $var;
output
total 20
drwxr-xr-x 5 drew.jess users 4096 2013-01-22 15:51 bash
drwxr-xr-x 2 drew.jess users 4096 2011-11-11 10:37 diff
drwxr-xr-x 8 drew.jess users 4096 2012-02-14 09:09 expect
drwxr-xr-x 3 drew.jess users 4096 2011-10-06 11:05 perl
drwxr-xr-x 3 drew.jess users 4096 2013-02-07 13:10 python