Re: blank lines as "^\s*$"

"RAKESH" <[email protected]>
Newsgroups gmane.editors.sed.user
Message-ID <[email protected]>

--- In [email protected], "Ruud H.G. van Tol" <rvtol@...> wrote:
>
> On 07/07/2013 09:38, RAKESH wrote:
> 
> > We could also do it making use of the -0 777 option of perl where
> > the whole file is slurped in, & then we proceed to
> > chop off the leading & trailing blank lines, like as if chopping the front/tails of a carrot.
> >
> > perl -0777wple 's/\A\s+//ms;s/\s+\z//ms' yourfile
> 
> That has issues:
> 
> - the 'ms' modifiers are superfluous
> - the starting whitespace of the first non-whitespace line is removed
> - the newline after the last non-whitespace line is removed
> 

That was a very good catch, (especially point #2),
I was not checking the code b4 posting.

perl -0777wple 's/\A\s*\n(?:\s*$)*//m;s/(?:^\s*$)*\z//m' yourfile

Or this:

perl -0777wple '1 while s/\A\s*\n//m;1 while s/^\s*\z//m' yourfile

As for the 'ms' modifier, I actually have a habit of putting them at the end of every regex that I write just to make me remember less their functionality. Actually, the "-l" option is redundant here as it's being set to the 0777 char in this case.

-Rakesh
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.