Today, thanks to a tweet from @moo_pronto (https://twitter.com/moo_pronto/status/622035032463527936) i noticed that FreeBSD's egrep is handling the regexp wrong.
echo "abc" | egrep -o '^[a-z]' should never also find "b" and "c", as it only should find the first character after the line beginning (^).
It would be okay if it was ^[a-z]+ but it's not. :)
I am very certain that is an error in regexp handling.
(Sorry if i ruined anyone's weekend)
% echo "abc" | bsdgrep -e "^[a-z]" -o
% echo "abc" | grep -e "^[a-z]" -o
bsdgrep gets it right, but grep does not. Our GPL'd grep must be old and has this bug?
-bash-3.2$ echo "abc" | grep -e "^[a-z]" -o
-bash-3.2$ grep -V
grep (GNU grep) 2.5.1
Copyright 1988, 1992-1999, 2000, 2001 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
-bash-4.1$ echo "abc" | grep -e "^[a-z]" -o
-bash-4.1$ grep -V
GNU grep 2.6.3
Copyright (C) 2009 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
grep went GPLv3 at 2.5.3, so we might not be able to backport the fix. Note that -o was a brand new feature in 2.5.1.
Perhaps we should just flip the switch and move to bsdgrep for 11-RELEASE?
Matching any four characters at the beginning of a line also fails:
% echo "1234 1234 1234" | egrep -o '^....'
Interestingly enough, this also fails with bsdgrep:
% echo "1234 1234 1234" | bsdgrep -o '^....'
Created attachment 172418 [details]
Patch for fixing anchor treatment in BSD grep.
The attached patch solves the bsdgrep that I reported in the previous comment. bsdgrep tries to find multiple matches by updating the starting offset after finding a match. However, the REG_NOTBOL flag should be set for offsets beyond 0, to ensure that regexec treats anchors correctly.
Patch for the same issue in OpenBSD's grep: https://marc.info/?l=openbsd-tech&m=147206598501753&w=2
grep also fails with case :
# echo AAA > test
# grep a test
# grep "[a-z]" test
# echo BBB | grep "[a-z]"
# grep -V
grep (GNU grep) 2.5.1-FreeBSD
# uname -r
Quite dangerous issue, could we think about having it corrected for FreeBSD 11 release ?
Note that B can be in the range a-z depending on collation settings:
You're right Daniël, I must face this.
What is strange is that on 2 different systems with the same LC_COLLATE, results are not the same.
Thank you for the link, and sorry for the noise...
Issues expressed in this thread exist with gnugrep, but none exist with bsdgrep at this point in time. This should be able to be closed with the installation of bsdgrep as /usr/bin/grep.