Re: blank lines as "^\s*$"
"RAKESH" <[email protected]>
| Newsgroups | gmane.editors.sed.user |
|---|---|
| Message-ID | <[email protected]> |
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
>