Bug in sed(1)?
Kenneth Almquist
ka at hropus.UUCP
Thu Jan 23 13:26:35 AEST 1986
> 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
More information about the Net.bugs.usg
mailing list