How to make BSD grep respect start-of-line anchor
Clash Royale CLAN TAG#URR8PPP
up vote
1
down vote
favorite
I'm trying to match the first character on a line with grep
, and print only that character. With GNU grep 2.20 in Linux I do something like this:
$ echo ab | grep -o '^.'
a
$
This is works as I expect it - the ^
anchors the regexp to the start of the line and only the a
character is printed.
However with BSD grep 2.5.1 on MacOS I get a different result:
$ echo ab | egrep -o '^.'
a
b
$
It is as if the ^
start-of-line anchor is being ignored. Interestingly the $
end-of-line anchor works as expected on both grep
flavours:
$ echo ab | grep -o '.$'
b
$
Interestingly, BSD grep does respect the ^
start-of-line anchor if the -o
option is not used:
$ echo a ; echo b; | grep '^a'
a
$
Does BSD grep have some other way to express
^
when-o
is used?Is there a portable way to express
^
when-o
is used that I can use with both Linux and MacOS?Is this a bug in BSD grep?
grep osx
add a comment |Â
up vote
1
down vote
favorite
I'm trying to match the first character on a line with grep
, and print only that character. With GNU grep 2.20 in Linux I do something like this:
$ echo ab | grep -o '^.'
a
$
This is works as I expect it - the ^
anchors the regexp to the start of the line and only the a
character is printed.
However with BSD grep 2.5.1 on MacOS I get a different result:
$ echo ab | egrep -o '^.'
a
b
$
It is as if the ^
start-of-line anchor is being ignored. Interestingly the $
end-of-line anchor works as expected on both grep
flavours:
$ echo ab | grep -o '.$'
b
$
Interestingly, BSD grep does respect the ^
start-of-line anchor if the -o
option is not used:
$ echo a ; echo b; | grep '^a'
a
$
Does BSD grep have some other way to express
^
when-o
is used?Is there a portable way to express
^
when-o
is used that I can use with both Linux and MacOS?Is this a bug in BSD grep?
grep osx
2
(echo ab; echo cd) | /usr/bin/egrep -o '^.'
on OpenBSD 6.2 givesa
and thenc
so it's probably at least a macOS bug
â thrig
Mar 5 at 18:29
@thrig interesting. I just tried FreeBSD 11 and your test yieldsanbncndn
. In that thegrep
version is2.5.1-FreeBSD
. What version does OpenBSD have?
â Digital Trauma
Mar 5 at 19:39
1
On OpenBSD 5.8, I getanbncndn
(as another data point).
â user4556274
Mar 5 at 20:11
A related question is unix.stackexchange.com/questions/398223 .
â JdeBP
Mar 6 at 8:31
add a comment |Â
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I'm trying to match the first character on a line with grep
, and print only that character. With GNU grep 2.20 in Linux I do something like this:
$ echo ab | grep -o '^.'
a
$
This is works as I expect it - the ^
anchors the regexp to the start of the line and only the a
character is printed.
However with BSD grep 2.5.1 on MacOS I get a different result:
$ echo ab | egrep -o '^.'
a
b
$
It is as if the ^
start-of-line anchor is being ignored. Interestingly the $
end-of-line anchor works as expected on both grep
flavours:
$ echo ab | grep -o '.$'
b
$
Interestingly, BSD grep does respect the ^
start-of-line anchor if the -o
option is not used:
$ echo a ; echo b; | grep '^a'
a
$
Does BSD grep have some other way to express
^
when-o
is used?Is there a portable way to express
^
when-o
is used that I can use with both Linux and MacOS?Is this a bug in BSD grep?
grep osx
I'm trying to match the first character on a line with grep
, and print only that character. With GNU grep 2.20 in Linux I do something like this:
$ echo ab | grep -o '^.'
a
$
This is works as I expect it - the ^
anchors the regexp to the start of the line and only the a
character is printed.
However with BSD grep 2.5.1 on MacOS I get a different result:
$ echo ab | egrep -o '^.'
a
b
$
It is as if the ^
start-of-line anchor is being ignored. Interestingly the $
end-of-line anchor works as expected on both grep
flavours:
$ echo ab | grep -o '.$'
b
$
Interestingly, BSD grep does respect the ^
start-of-line anchor if the -o
option is not used:
$ echo a ; echo b; | grep '^a'
a
$
Does BSD grep have some other way to express
^
when-o
is used?Is there a portable way to express
^
when-o
is used that I can use with both Linux and MacOS?Is this a bug in BSD grep?
grep osx
asked Mar 5 at 18:19
Digital Trauma
5,47811528
5,47811528
2
(echo ab; echo cd) | /usr/bin/egrep -o '^.'
on OpenBSD 6.2 givesa
and thenc
so it's probably at least a macOS bug
â thrig
Mar 5 at 18:29
@thrig interesting. I just tried FreeBSD 11 and your test yieldsanbncndn
. In that thegrep
version is2.5.1-FreeBSD
. What version does OpenBSD have?
â Digital Trauma
Mar 5 at 19:39
1
On OpenBSD 5.8, I getanbncndn
(as another data point).
â user4556274
Mar 5 at 20:11
A related question is unix.stackexchange.com/questions/398223 .
â JdeBP
Mar 6 at 8:31
add a comment |Â
2
(echo ab; echo cd) | /usr/bin/egrep -o '^.'
on OpenBSD 6.2 givesa
and thenc
so it's probably at least a macOS bug
â thrig
Mar 5 at 18:29
@thrig interesting. I just tried FreeBSD 11 and your test yieldsanbncndn
. In that thegrep
version is2.5.1-FreeBSD
. What version does OpenBSD have?
â Digital Trauma
Mar 5 at 19:39
1
On OpenBSD 5.8, I getanbncndn
(as another data point).
â user4556274
Mar 5 at 20:11
A related question is unix.stackexchange.com/questions/398223 .
â JdeBP
Mar 6 at 8:31
2
2
(echo ab; echo cd) | /usr/bin/egrep -o '^.'
on OpenBSD 6.2 gives a
and then c
so it's probably at least a macOS bugâ thrig
Mar 5 at 18:29
(echo ab; echo cd) | /usr/bin/egrep -o '^.'
on OpenBSD 6.2 gives a
and then c
so it's probably at least a macOS bugâ thrig
Mar 5 at 18:29
@thrig interesting. I just tried FreeBSD 11 and your test yields
anbncndn
. In that the grep
version is 2.5.1-FreeBSD
. What version does OpenBSD have?â Digital Trauma
Mar 5 at 19:39
@thrig interesting. I just tried FreeBSD 11 and your test yields
anbncndn
. In that the grep
version is 2.5.1-FreeBSD
. What version does OpenBSD have?â Digital Trauma
Mar 5 at 19:39
1
1
On OpenBSD 5.8, I get
anbncndn
(as another data point).â user4556274
Mar 5 at 20:11
On OpenBSD 5.8, I get
anbncndn
(as another data point).â user4556274
Mar 5 at 20:11
A related question is unix.stackexchange.com/questions/398223 .
â JdeBP
Mar 6 at 8:31
A related question is unix.stackexchange.com/questions/398223 .
â JdeBP
Mar 6 at 8:31
add a comment |Â
1 Answer
1
active
oldest
votes
up vote
1
down vote
Digging a bit deeper, I found this behavior reported in a FreeBSD bug:
I've noticed some more issues with the same version of grep. I don't
know whether they're related, but I'll append them here for now.
$ printf abc | grep -o '^[a-c]'
should just print 'a', but instead gives three hits, against each letter
of the incoming text.
But it's not clear to me if or when this will be fixed.
It seems clear to me that the base system grep on FreeBSD is GNU grep and that there are plans to replace it within a short enough time (a couple of years or so) for them not to bother fixing the bug right now at least.
â Kusalananda
Mar 5 at 21:07
The questioner is using MacOS, not FreeBSD, Kusalananda.
â JdeBP
Mar 6 at 8:34
add a comment |Â
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
Digging a bit deeper, I found this behavior reported in a FreeBSD bug:
I've noticed some more issues with the same version of grep. I don't
know whether they're related, but I'll append them here for now.
$ printf abc | grep -o '^[a-c]'
should just print 'a', but instead gives three hits, against each letter
of the incoming text.
But it's not clear to me if or when this will be fixed.
It seems clear to me that the base system grep on FreeBSD is GNU grep and that there are plans to replace it within a short enough time (a couple of years or so) for them not to bother fixing the bug right now at least.
â Kusalananda
Mar 5 at 21:07
The questioner is using MacOS, not FreeBSD, Kusalananda.
â JdeBP
Mar 6 at 8:34
add a comment |Â
up vote
1
down vote
Digging a bit deeper, I found this behavior reported in a FreeBSD bug:
I've noticed some more issues with the same version of grep. I don't
know whether they're related, but I'll append them here for now.
$ printf abc | grep -o '^[a-c]'
should just print 'a', but instead gives three hits, against each letter
of the incoming text.
But it's not clear to me if or when this will be fixed.
It seems clear to me that the base system grep on FreeBSD is GNU grep and that there are plans to replace it within a short enough time (a couple of years or so) for them not to bother fixing the bug right now at least.
â Kusalananda
Mar 5 at 21:07
The questioner is using MacOS, not FreeBSD, Kusalananda.
â JdeBP
Mar 6 at 8:34
add a comment |Â
up vote
1
down vote
up vote
1
down vote
Digging a bit deeper, I found this behavior reported in a FreeBSD bug:
I've noticed some more issues with the same version of grep. I don't
know whether they're related, but I'll append them here for now.
$ printf abc | grep -o '^[a-c]'
should just print 'a', but instead gives three hits, against each letter
of the incoming text.
But it's not clear to me if or when this will be fixed.
Digging a bit deeper, I found this behavior reported in a FreeBSD bug:
I've noticed some more issues with the same version of grep. I don't
know whether they're related, but I'll append them here for now.
$ printf abc | grep -o '^[a-c]'
should just print 'a', but instead gives three hits, against each letter
of the incoming text.
But it's not clear to me if or when this will be fixed.
answered Mar 5 at 19:53
Digital Trauma
5,47811528
5,47811528
It seems clear to me that the base system grep on FreeBSD is GNU grep and that there are plans to replace it within a short enough time (a couple of years or so) for them not to bother fixing the bug right now at least.
â Kusalananda
Mar 5 at 21:07
The questioner is using MacOS, not FreeBSD, Kusalananda.
â JdeBP
Mar 6 at 8:34
add a comment |Â
It seems clear to me that the base system grep on FreeBSD is GNU grep and that there are plans to replace it within a short enough time (a couple of years or so) for them not to bother fixing the bug right now at least.
â Kusalananda
Mar 5 at 21:07
The questioner is using MacOS, not FreeBSD, Kusalananda.
â JdeBP
Mar 6 at 8:34
It seems clear to me that the base system grep on FreeBSD is GNU grep and that there are plans to replace it within a short enough time (a couple of years or so) for them not to bother fixing the bug right now at least.
â Kusalananda
Mar 5 at 21:07
It seems clear to me that the base system grep on FreeBSD is GNU grep and that there are plans to replace it within a short enough time (a couple of years or so) for them not to bother fixing the bug right now at least.
â Kusalananda
Mar 5 at 21:07
The questioner is using MacOS, not FreeBSD, Kusalananda.
â JdeBP
Mar 6 at 8:34
The questioner is using MacOS, not FreeBSD, Kusalananda.
â JdeBP
Mar 6 at 8:34
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f428340%2fhow-to-make-bsd-grep-respect-start-of-line-anchor%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
2
(echo ab; echo cd) | /usr/bin/egrep -o '^.'
on OpenBSD 6.2 givesa
and thenc
so it's probably at least a macOS bugâ thrig
Mar 5 at 18:29
@thrig interesting. I just tried FreeBSD 11 and your test yields
anbncndn
. In that thegrep
version is2.5.1-FreeBSD
. What version does OpenBSD have?â Digital Trauma
Mar 5 at 19:39
1
On OpenBSD 5.8, I get
anbncndn
(as another data point).â user4556274
Mar 5 at 20:11
A related question is unix.stackexchange.com/questions/398223 .
â JdeBP
Mar 6 at 8:31