[net.bugs.usg] Bug in sed

ka@hropus.UUCP (Kenneth Almquist) (01/23/86)

> I have encountered what I THINK is a bug in sed(1).  I would appreciate it
> if some wizard more knowing that I would apprise me as to whether it is a
> bug or a feature (and whether there is a fix).
>
> Specifically, it involves expressions of the form '1,/^fixed_pattern$/d'
> which when used as the only argument to sed should cause all input lines
> from the beginning to the first one which matches fixed_pattern exactly
> to be deleted, and the remainder to be printed to stdout.

The manual states that:

    A command line with two addresses selects the inclusive
    range from the first pattern space that matches the first
    address through the next pattern space that matches the
    second.

Thus when you say "1,/pattern/d", the first address matches the
line 1 of the file, and then checks for lines matching the
second address (the /pattern/) starting with line 2 ("the next
pattern space" as opposed to the current pattern space).  This
means that in general if you give two addresses on a sed
command, at least two lines will be matched.  Silly, you say?
Wait 'til you see the *next* sentence:

    (If the second address is a number less than or equal to
    the line number first selected, only one line is selected.)

Thus it is possible to match only one with two addresses; "5,5d"
works as expected.  But "5,4d" also deletes line 5!

Since it's documented I would classify it as a "feature", however
silly and unwanted it may be...

I don't believe there is any work-around if you restrict yourself
to sed.  Using awk, you can say

	awk 'flag != 0; /pattern/ {flag = 1}'

				Kenneth Almquist
				houxm!hropus!ka

sam@delftcc.UUCP (Sam Kendall) (01/25/86)

In article <685@ttrdc.UUCP>, levy@ttrdc.UUCP (Daniel R. Levy) writes:
> I have encountered what I THINK is a bug in sed(1)....
> 
> Specifically, it involves expressions of the form '1,/^fixed_pattern$/d'....
> 
> Unfortunately, this seems to fail when the first line of the input exactly
> matches fixed_pattern; then, instead of only the first line of the input
> being discarded, ALL the lines of input are discarded.

As another news item noted, this is documented behavior.  To work around
it, prepend a blank line to the input stream:

	( echo; cat ) | sed '1,/^fixed_pattern$/d'

This does exactly what you want.

I think it is possible to work around this in a single sed process,
using the pattern space, but I the result would be very ugly!

----
Sam Kendall			     allegra \
Delft Consulting Corp.		seismo!cmcl2  ! delftcc!sam
+1 212 243-8700			       ihnp4 /
ARPA: delftcc!sam@nyu.ARPA