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.
A commit references this bug:
Date: Tue Dec 8 14:05:26 UTC 2020
New revision: 368439
src.opts.mk: switch to bsdgrep as /usr/bin/grep
This has been years in the making, and we all knew it was bound to happen
some day. Switch to the BSDL grep implementation now that it's been a
little more thoroughly tested and theoretically supports all of the
extensions that gnugrep in base had with our libregex(3).
Folks shouldn't really notice much from this update; bsdgrep is slower than
gnugrep, but this is currently the price to pay for fewer bugs. Those
dissatisfied with the speed of grep and in need of a faster implementation
should check out what textproc/ripgrep and textproc/the_silver_searcher
can do for them.
I have some WIP to make bsdgrep faster, but do not consider it a blocker
when compared to the pros of switching now (aforementioned bugs, licensing).
PR: 228798 (exp-run)
PR: 128645, 156704, 166842, 166862, 180937, 193835, 201650
PR: 232565, 242308, 246000, 251081, 191086, 194397
Relnotes: yes, please