bug-sed
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

bug#19899: deleting lines of a file with sed - unexpected behaviour


From: Norihiro Tanaka
Subject: bug#19899: deleting lines of a file with sed - unexpected behaviour
Date: Fri, 20 Feb 2015 02:13:58 +0900

On Wed, 18 Feb 2015 19:56:58 -0500
Ethan Kaufman <address@hidden> wrote:

> To whom it may concern,
> 
> I noticed something odd while fooling around with sed.  If you try to
> remove multiple line intervals (by number) from a file, but any interval
> specified later in the list is a subset of an interval earlier in the list,
> then an additional single line is removed after the specified (larger)
> interval.
> 
> seq 10 > foo.txt
> 
> sed '2,7d;3,6d' foo.txt
> 1
> 9
> 10
> 
> Expected output is:
> 
> 1
> 8
> 9
> 10
> 
> Additional tests:
> 
> For each additional redundant interval, another line is removed:
> sed '2,7d;3,6d;4,5d' foo.txt
> 1
> 10
> 
> Reversing the order of the intervals produces the expected result!
> sed '3,6d;2,7d' foo.txt
> 1
> 8
> 9
> 10
> 
> Specifying the intervals with '-e' produces the same result:
> sed -e '2,7d' -e '3,6d' foo.txt
> 1
> 9
> 10
> 
> Using different interval syntax has mixed results:
> sed -e '/2/,/7/d' -e '/3/,/6/d' foo.txt
> 1
> 8
> 9
> 10
> sed -e '2,7d' -e '/3/,/6/d' foo.txt
> 1
> 8
> 9
> 10
> sed -e '/2/,/7/d' -e '3,6d' foo.txt
> 1
> 9
> 10
> 
> Trailing list must be a subset for the additional line to be removed:
> 
> sed '2,5d;1,5d'
> 1
> 8
> 9
> 10
> sed '2,5d;2,6d'
> 1
> 8
> 9
> 10
> sed '2,5d;2,5d'
> 1
> 9
> 10
> 
> Versions:
> 
> Breakage appears to have occurred in the 4.1 release.  See the expected
> output for all cases in GNU sed 3.02 and 4.09 (as well as BSD sed (Mac OS X
> 10.2 Yosemite and /bin/sed on Solaris), but not in 4.15 and 4.21.
> 
> This issue and above information has been discussed on stack overflow:
> stackoverflow.com/questions/28595574/deleting-lines-of-a-file-with-sed-unexpected-behaviour
> 
> Cheers,
> Ethan

Hi Ethan,

I see that the behavior is a bug.  I tested following sed-4.2.2, old GNU
sed, sed on Solaris 11 and sed on HP-UX, and sed-4.2.2 only does not
return '8'.

  $ sed '2,7d;3,6d' foo.txt

When line number addresses are overwrapped in two editing commands,
sed always executes for at least one line with the second edition
command, even if out of between two addresses.  e.g. for '2,7d;3,6d',
sed executes for line 8 which should not be executed for.

Thanks,
Norihiro

Attachment: 0001-sed-fix-mishandle-with-overwrapped-line-number-addre.patch
Description: Text document


reply via email to

[Prev in Thread] Current Thread [Next in Thread]