Subject: Re: bug#30829: (no subject)

From: Assaf Gordon <[email-masked]>
Date: Thu, 15 Mar 2018 18:16:48 -0600

Hello,

On Fri, Mar 16, 2018 at 12:09:05AM +0100, Don Crissti wrote:
[...] 
> However, my code uses empty regex on condition (only for a certain range of lines). It is logical that '//!d' should not be executed for lines outside that range. If I used a plain 'd' instead of '//!d' would sed unconditionally delete all lines from the file ? No. It would delete only the lines in that range. Similarly, sed should not even attempt to evaluate the empty regex in '{//!d' for lines outside that range.
> 
> Unless I'm missing something I still see this as a bug.

There is a subtle issue here:
when using 2 addresses, and the second address is an RE,
the first line matching the first address (line 2 in your case)
will *never* be checked against the RE.

And so, even though the '//!d' is run conditionally,
the condition is true (line 2, before regex is checked),
and then '//!d' is executed but there is no 'last regex' yet.

This is documented in the manual:
https://www.gnu.org/software/sed/manual/sed.html#Range-Addresses
(in the second paragraph, starting with "if the second address is a regexp").

Also,
In the POSIX standard, the relevant text is:
http://pubs.opengroup.org/onlinepubs/9699919799/utilities/sed.html
(under "addresses in sed" section).

  "An editing command with two addresses shall select the inclusive 
  range from the first pattern space that matches the first address 
  through the next pattern space that matches the second. "

The interpretation is that the second address is checked against
"the next pattern space" - impling that the first time the second
address is checkd is not in the first line that matches, but
on the 'next pattern space' (meaning starting at the line following
the line that matched the first address).
[phew, that is a bit confusing....]

Does this clarify the issue?

-assaf