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

"RAKESH" <[email protected]>
Newsgroups gmane.editors.sed.user
Message-ID <[email protected]>
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

But since it's a sed forum so ... sed rules, atleast here ;-)

-Rakehs


--- In [email protected], Logan Palanisamy <lpalani@...> wrote:
>
> Another way to solve this problem is to use "tac" two times. tac lists the contents reverse order (opposite of cat) 
> 
> 
> $sed -e '/[^ \t]/,$!d' input_file.txt | tac | sed '/[^ \t]/,$!d' | tac
> 
> Steps: Trim the leading blank lines, pipe the output to tac to reverse the lines, trim the leading blank lines which used to be the trailing blank lines, pipe the output to tac again to get the original order.
> 
> 
> -----Original Message-----
> From: [email protected] [mailto:[email protected]] On Behalf Of Daniel
> Sent: Saturday, July 06, 2013 11:14 AM
> To: [email protected]
> Subject: Re: blank lines as "^\s*$"
> 
> That is really great.
> 
> The first part (remove leading blank lines) is clear to me: "Find the range from the first non-blank line to the end of the stream. Delete the opposite range (any leading blank lines)". 
> 
> The second part (removes trailing blank lines) is less clear, even after using sedsed some. It works in all the test cases I tried. But can you explain a little how / why it works?
> 
> Thanks,
> Daniel
> 
> --- In [email protected], "RAKESH" <sharma__r@> wrote:
> >
> > 
> > After posting the reply I saw the typo(line-2), & then while fixing it re-wrote the regex.
> > sed -e '
> > /[^ \t]/,$!d
> > /[^ \t]/b
> > :a
> > $d
> > N
> > /\n[ \t]*$/ba
> > ' yourfile
> > 
> > 
> > --- In [email protected], "RAKESH" <sharma__r@> wrote:
> > >
> > > 
> > > Thanks for the detailed distinctions in blank lines vs. the empty ones. Although I was aware of the differences(hence mentioned upfront my assumptions), and chose to use the /^$/ variety since it doesn't get in the way of the sed code in terms of legibility.
> > > 
> > > recapitulating, (i.e., /^$/ based )
> > > #-----------------------------
> > > /./,$!d
> > > /./b
> > > :a
> > > $d
> > > N
> > > /\n\n*$/ba
> > > #-----------------------------
> > > 
> > > Now this is how the above would appear using the /^\s*$/ approach
> > > Note: repl. \t => literal TAB since "sed" doesn't support \t and 
> > > there's no way to show it's a TAB otherwise in here.
> > > #------------------------------------------
> > > /[^ \t]/,$!d
> > > /[^ \t/b
> > > :a
> > > $d
> > > N
> > > h
> > > s/.*\n//
> > > /^[ \t]*$/{
> > > g;ba
> > > }
> > > g
> > > #------------------------------------------
> > > 
> > > Notice the complexity increase in this case, which is due to fact that "sed" doesn't come armed with the \s \S regexes, otherwise this would have been a breeze, as you yourself state.
> > > 
> > > Just so that the OP gets an idea of a sed-based flow is why I chose the code that I gave. Once the hang of that is got, then the OP can upgrade to the /^\s*$/ approach if needed.
> > > 
> > > Rakehs
> > > 
> > > --- In [email protected], Sven Guckes <maillists-yahoo@> wrote:
> > > >
> > > > * Rakesh Sharma <sharma__r@> [2013-07-05 14:15]:
> > > > > Note: blank line => /^$/ i.e.,
> > > > > one which has no characters,
> > > > > not even spaces &/or TABs in it.
> > > > 
> > > > well, a line with *no* characters in it i'd call "empty".
> > > > 
> > > > so - here's my definition of "blank lines":
> > > > a "blank line" is contains only "blank characters"; "blank 
> > > > characters" are characters which do not show up with any dots 
> > > > (unless a font gives them any dots).
> > > > 
> > > > an empty line is a special case of a blank line because all 
> > > > characters contained are "blank".
> > > > it is certainly valid for every contained element as there are 
> > > > none for which it requires such quality.
> > > > (okay.. a little bit of set logic here..)
> > > > 
> > > > you may define a subset of these blank characters to be valid 
> > > > only, eg spaces and tabs.
> > > > 
> > > > in the editor vim you can use the pattern "\s" for these.
> > > > (i think the notation of '\s' has been taken from "perl compatible 
> > > > regular expressions" aka PCRE.)
> > > > 
> > > > so "blank lines" match the pattern "^\s*$"
> > > > which also matches with empty lines.
> > > > 
> > > > too bad we dont have "anchors" for "start of data" and "end of 
> > > > data".
> > > > these would probably make the
> > > > given problem a breeze.
> > > > 
> > > > Sven
> > > > 
> > > > --
> > > > $ man 7 regex
> > > > Standard character class names are:
> > > > alnum alpha blank cntrl digit graph lower print punct space upper 
> > > > xdigit
> > > >
> > >
> >
> 
> 
> 
> 
> ------------------------------------
> 
> --
> Yahoo! Groups Links
>
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.