On Sat, 2021-08-14 at 20:20 +0200, Andreas Kusalananda Kähäri wrote:
> On Fri, Aug 13, 2021 at 11:19:16PM +0800, Philippe Meunier wrote:
> > Hello,
> > 
> > While porting a shell script from Linux to OpenBSD I came across the
> > following:
> [cut] 
> > 2) Out of curiosity, is there an OpenBSD equivalent to GNU's '0,/^test$/d' ?
> 
> As far as I can see, the following would work the same as GNU's '0,/^test$/d':
> 
>       sed -e '1 { /^test$/d; }' -e '1,/^test$/d' file
> 
> That is, delete the first line if it is "test", otherwise delete from
> line 1 the next line that is "test".
> 
This behaviour should not be relied upon for portable scripts, since
addresses in combination with "next cycle" are heavily underspecified
and at least doesn't match gsed's behaviour:
$ printf 'test1\nbla1\ntest2\nbla2\n' | gsed -e '1 { /^test/d; }' -e 
'1,/^test/d'
bla2

Right now, I can't for the life of me think of a shorter way to do this
portably, but the code below works in both sed and gsed.
$ printf 'bla0\ntest1\nbla1\ntest2\nbla2\n' | sed 
'1{/^test/{h;d;};};1,/^test/{x;/^$/{x;d;};x;}'  
bla1
test2
bla2
$ printf 'bla0\ntest1\nbla1\ntest2\nbla2\n' | gsed 
'1{/^test/{h;d;};};1,/^test/{x;/^$/{x;d;};x;}'
bla1
test2
bla2
$ printf 'test1\nbla1\ntest2\nbla2\n' | sed 
'1{/^test/{h;d;};};1,/^test/{x;/^$/{x;d;};x;}'       
bla1
test2
bla2
$ printf 'test1\nbla1\ntest2\nbla2\n' | gsed 
'1{/^test/{h;d;};};1,/^test/{x;/^$/{x;d;};x;}'
bla1
test2
bla2
If you need this snippet in a larger script that needs the holdspace,
make sure that you clean the holdspace before continuing.

martijn@

Reply via email to