sed behaving strangely when -n and the delete command are combined

Ralf Friedl Ralf.Friedl at online.de
Wed Jul 8 13:23:31 UTC 2009


Rob Landley wrote:
> On Tuesday 30 June 2009 07:27:21 Maximilian Gaß wrote:
>   
>> sed -n -e '1d' -e '1,/@HEADER_ENDS/p' initfini.s
>>
>> Busybox sed gives no output,  while GNU sed gives the expected
>>     
>
> It's expected?
>
> If you deleted your ending search condition before evaluating it, would you 
> expect your search to stop?  So why do you expect your search to start when 
> you deleted your starting condition before evaluating it?  (I believe that 
> according to susv3, delete ends evaluation of the sed script and jumps 
> immediately to the next line.  So your second -e doesn't get run for line 1, 
> therefore it doesn't start the range.)
>
> Looking at other kinds of addresses, if you did:
>
>   sed -n -e '1d' -e '/@HEADER_STARTS/,/@HEADER_ENDS/p' initfini.s
>
> And line 1 was "@HEADER_STARTS", would you expect your range to start anyway?  
> If not, why do you expect a numerical start address to work differently?
>   
Because /@HEADER_STARTS/ refers to a line containing @HEADER_STARTS, 
while 1 refers to the first line, independent of its content.

> Ok, let's look at the spec.
>
> According to http://www.opengroup.org/onlinepubs/9699919799/toc.htm
>
>   An address is either a decimal number that counts input lines cumulatively
>   across files, a '$' character that addresses the last line of input, or a
>   context address (which consists of a BRE, as described in Regular
>   Expressions in sed , preceded and followed by a delimiter, usually a
>   <slash>).
>
>   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. (If the second address is a number
>   less than or equal to the line number first selected, only one line shall be
>   selected.) Starting at the first line following the selected range, sed shall
>   look again for the first address. Thereafter, the process shall be repeated.
>
>   d is "Delete the pattern space and start the next cycle".
>
> Deleting the pattern space starts the next cycle, so the first address never 
> got matched, so the range didn't start...
>   
The question here is, after line number 1 is deleted, is there no longer 
a line with number 1, or does line number 2 become line number 1?

The standard you quote does not address this question, but let's assume 
you were using vi with the commands ":1d" followed by ":1,$d". The first 
command deletes line 1. By your reasoning, the second command should not 
match, because line 1 is already deleted by the first command. Actually, 
the second command deletes from what was line 2 in the original file to 
the end. After both commands the entire contents of the file are 
deleted. And I tried this with busybox vi.

Regards
Ralf


More information about the busybox mailing list