Re: Print ranges without its BEGIN and END patterns

"'Seeger, Stephan' [email protected] [sed-users]" <[email protected]>
Newsgroups gmane.editors.sed.user
Message-ID <[email protected]>
@Tim: Thank you for your detailed explanation - now I see a whole lot clearer. Really looks like an awesome hack compared to the variants seen on the internet. Also it seems to be a completely new invention when even the sed FAQ at sourceforge i.e. 4.24 doesn't mention anything alike...probably they should extend them now.
From your description I conclude that the statement must be fairly robust and portable or do you think any of the logic it relys on will be changed/ unavailable in some sed version.


Did I also get it right, that due to...


----snip, from sed manual---


...../regexp/
This will select any line which matches the regular expression regexp. If regexp itself includes any / characters, each must be escaped by a backslash (\).


The empty regular expression ‘//’ repeats the last regular expression match (the same holds if the empty regular expression is passed to the scommand). Note that modifiers to regular expressions are evaluated when the regular expression is compiled, thus it is invalid to specify them together with the empty regular expression.


----snip---


...{//!p} means "not print last regular expression match (and that's what the range BEGIN/END patterns essentially are)"?


Best regards


Stephan








Am 05.03.2015 um 17:16 schrieb Tim Chase [email protected]<mailto:[email protected]> [sed-users] <[email protected]<mailto:[email protected]>>:






On 2015-03-05 14:21, 'Seeger, Stephan'
> I'm really new to using sed. While experimenting in order to print
> ranges without its BEGIN and END patterns I accidentially came
> across that given a string variable:


New to sed and you stumble upon this awesome hack?! Nicely done! I
don't think I've ever seen this one before


> sed -n '/second/,/fourth/{//!p}'
[snip]
> Unfortunately I didn't find this described anywhere in manuals nor
> the internet. Probably there's anyone out there who can explain
> whether this is a bug or a feature...and how/why it works.


As best I can tell, this is working as a side-effect of documented
behavior. The general process/flow would be something like


1) check if this line matches the start-of-range pattern
("second"). As a side-effect of this, the "last pattern" gets set to
the same thing and we internally annotate that we're inside this
range.


2) it matches, so do the stuff inside the "{...}"


3) Check to see if the current line matches the "last
pattern" ("//"). It does, so we don't ("!") execute the print command


4) proceed to the next line


5) we're inside the range, so we check for the end-of-range pattern.
As a side-effect, the "last pattern" gets set. It doesn't match, so
we continue to execute the "{...}"


6) we check if "third" matches the "last pattern" (now the
end-of-range pattern). It doesn't, so we print it


7) we're inside the range, so we check for the end of the range.

As a side-effect, the "last pattern" gets set. It does match, so
we continue to execute the "{...}", and then unset the internal
"within this range" flag


8) executing the "{...}", we check if "fourth" matches the "last
pattern". It does, so we don't print it.


9) we're now outside the range, so we resume looking for "second"
and


It doesn't even break if the start-pattern appears within the range:


$ printf '%s\n' a b c d e d f g h i j | sed -n '/d/,/g/{//!p}'
e
d
f


(note that "d" does come between the opening "d" and the closing "g",
so it gets printed)


So I'd claim that this is some of the most elegant sed hackery I've
seen in a while (at least for such a short piece of code).


-tim










[Non-text portions of this message have been removed]




------------------------------------

------------------------------------

-- 

------------------------------------

Yahoo Groups Links

<*> To visit your group on the web, go to:
    http://groups.yahoo.com/group/sed-users/

<*> Your email settings:
    Individual Email | Traditional

<*> To change settings online go to:
    http://groups.yahoo.com/group/sed-users/join
    (Yahoo! ID required)

<*> To change settings via email:
    [email protected] 
    [email protected]

<*> To unsubscribe from this group, send an email to:
    [email protected]

<*> Your use of Yahoo Groups is subject to:
    https://info.yahoo.com/legal/us/yahoo/utos/terms/
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.