busybox sed // behaviour differs from GNU

Jonathan H N Chin busybox at jhnc.org
Wed Nov 20 07:05:56 UTC 2024


The standard says:

> 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.

and:

>  If an RE is empty (that is, no pattern is specified) sed shall behave
>  as if the last RE used in the last command applied (either as an address
>  or as part of a substitute command) was specified.

https://pubs.opengroup.org/onlinepubs/9799919799/utilities/sed.html


With GNU sed (ubuntu 24.04 4.9-2build1):
```
$ seq 1 5 | sed -n '/1/,/3/{//p}'
1
3
$ seq 1 5 | sed -n '/1/,/3/h; //p; d'
1
3
$ seq 31 35 | sed -n '/1/,/3/{//p}'
31
32
$ seq 31 35 | sed -n '/1/,/3/h; //p; d'
31
32
```

but with busybox sed (ubuntu 24.04 1:1.36.1-6ubuntu3.1):
```
$ seq 1 5 | busybox sed -n '/1/,/3/{//p}'
3
$ seq 1 5 | busybox sed -n '/1/,/3/h; //p; d'
3
$ seq 31 35 | busybox sed -n '/1/,/3/{//p}'
31
32
$ seq 31 35 | busybox sed -n '/1/,/3/h; //p; d'
31
32
33
34
35
```

A barebones busybox compiled from 1_37_0 distribution tarball gives
me the same result.

"last command" seems incompletely defined by the standard.
Which iteration of the range should `{//p}` refer to?
Tests 2 and 4 sidestep this ambiguity by moving the // outside
the range command.

I would expect the 2nd test to:

* check line 1 for /1/ - "1" matches, so // succeeds
* check line 2 for /3/ - "2" fails, so // fails
* check line 3 for /3/ - "3" matches, so // succeeds
* check line 4 for /1/ - "4" fails, so // fails
* check line 5 for /1/ - "5" fails, so // fails

I would expect the 4th test to:

* check line 1 for /1/ - "31" matches, so // succeeds
* check line 2 for /3/ - "32" matches, so // succeeds
* check line 3 for /1/ - "33" fails, so // fails
* check line 4 for /1/ - "34" fails, so // fails
* check line 5 for /1/ - "35" fails, so // fails


-jonathan



More information about the busybox mailing list