Verified; this does look like a bug in Apple and/or FreeBSD grep. (This is completely unsurprising to me; FreeBSD grep is a standards-hating pile of poo. My employer installs GNU grep on all our Mac systems rather than put up with FreeBSD's wacky behavior. And yes, GNU grep finds both lines in your example.)
If portability between Mac and Linux is an objective, I strongly recommend abandoning grep
altogether and just using perl
(or something like ack
, which depends on perl
).
@akostadinov's suggestion to put longer patterns before shorter ones in f2.txt does seem to work around the bug, but I would be leery of relying on something like that. It works on this toy example, but does it work in general, and how expensive will it be when you find out?