Re: to remove leading and trailing blank lines
"Ruud H.G. van Tol" <[email protected]>
| Newsgroups | gmane.editors.sed.user |
|---|---|
| Message-ID | <[email protected]> |
On 01/07/2013 21:11, Jim Hill wrote:
> I figure buffering's too cheap to meter, so I like this one:
>
> 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