Discussion:
to remove leading and trailing blank lines
Jim Hill
2013-07-01 19:11:21 UTC
Permalink
I figure buffering's too cheap to meter, so I like this one:

sed 'H;$!d;g; s/\n*$//; s/^\n*//'
Ruud H.G. van Tol
2013-07-07 07:52:25 UTC
Permalink
Post by Jim Hill
sed 'H;$!d;g; s/\n*$//; s/^\n*//'
A line-oriented Perl version, with some state, and a DEBUG mode:

echo -e "\n \n\nx\n \n\ny\n\nz\n \n\n" | DEBUG=0 perl -ne'
$s ||= /\S/ or next; # skip heading empty lines
$b .= $_; # buffer lines
/\S/ or next; # until non-whitespace
$b =~ s/^(.*)$/<$1>/mg if $ENV{DEBUG};
print($b); # print buffer
$b = ""; # empty buffer
'
x


y

z
--
Ruud
Loading...