Discussion:
sed vs gsed and \+ difference
(too old to reply)
Jiri B
2016-07-28 10:29:06 UTC
Permalink
Hi,

I can't understand a difference between OpenBSD and GNU sed when
handling '\+' (one or more).

Example:

$ echo 'tzdata-2016a-1.el7.noarch.rpm' | sed 's/\(tzdata\)\+.*/\1/'
tzdata-2016a-1.el7.noarch.rpm
$ echo 'tzdata-2016a-1.el7.noarch.rpm' | gsed 's/\(tzdata\)\+.*/\1/'
tzdata

$ echo '1123456' | sed -n '/1\+23456/p'
$ echo '1123456' | gsed -n '/1\+23456/p'
1123456

A bug or some hidden trick?

j.
David Vasek
2016-07-28 10:55:40 UTC
Permalink
Post by Jiri B
Hi,
I can't understand a difference between OpenBSD and GNU sed when
handling '\+' (one or more).
$ echo 'tzdata-2016a-1.el7.noarch.rpm' | sed 's/\(tzdata\)\+.*/\1/'
tzdata-2016a-1.el7.noarch.rpm
$ echo 'tzdata-2016a-1.el7.noarch.rpm' | gsed 's/\(tzdata\)\+.*/\1/'
tzdata
$ echo '1123456' | sed -n '/1\+23456/p'
$ echo '1123456' | gsed -n '/1\+23456/p'
1123456
A bug or some hidden trick?
A bug in you example. Character '\+' is a plus, not "one or more".

For "one or more" you need '+' and extended regular expressions turned on
with option -E. GNU sed probably has a different opinion about what
operators are basic RE. See re_format(7).

Regards,
David
Jiri B
2016-07-28 11:06:29 UTC
Permalink
Post by David Vasek
Post by Jiri B
Hi,
I can't understand a difference between OpenBSD and GNU sed when
handling '\+' (one or more).
$ echo 'tzdata-2016a-1.el7.noarch.rpm' | sed 's/\(tzdata\)\+.*/\1/'
tzdata-2016a-1.el7.noarch.rpm
$ echo 'tzdata-2016a-1.el7.noarch.rpm' | gsed 's/\(tzdata\)\+.*/\1/'
tzdata
$ echo '1123456' | sed -n '/1\+23456/p'
$ echo '1123456' | gsed -n '/1\+23456/p'
1123456
A bug or some hidden trick?
A bug in you example. Character '\+' is a plus, not "one or more".
For "one or more" you need '+' and extended regular expressions turned on
with option -E. GNU sed probably has a different opinion about what
operators are basic RE. See re_format(7).
$ echo 'tzdata-2016a-1.el7.noarch.rpm' | sed -E 's/(tzdata)+.*/\1/'
tzdata
$ echo '1123456' | sed -En '/1+23456/p'
1123456

Thank you!

j.

Loading...