How to make BSD grep respect start-of-line anchor

The name of the pictureThe name of the pictureThe name of the pictureClash 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?







share|improve this question
















  • 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










  • @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




    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














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?







share|improve this question
















  • 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










  • @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




    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












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?







share|improve this question












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?









share|improve this question











share|improve this question




share|improve this question










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







  • 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












  • 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










  • @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




    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







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










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.






share|improve this answer




















  • 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










Your Answer







StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "106"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: false,
noModals: false,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);








 

draft saved


draft discarded


















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






























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.






share|improve this answer




















  • 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














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.






share|improve this answer




















  • 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












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.






share|improve this answer












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.







share|improve this answer












share|improve this answer



share|improve this answer










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
















  • 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












 

draft saved


draft discarded


























 


draft saved


draft discarded














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













































































Popular posts from this blog

How to check contact read email or not when send email to Individual?

Bahrain

Postfix configuration issue with fips on centos 7; mailgun relay