I'm trying to use a grep command with the –only-matching flag, but it's not behaving as I would expect it to.
This command:
echo "1/2/3/4/5" | grep -oE "^([^/]+/){0,2}"
Gives this output:
1/2/
3/4/
I was expecting just 1/2/
What's going on..? 3/4/
shouldn't match "^([^/]+/){0,2}"
because it's not at the beginning of the line..
(running GNU grep 2.5.1)
Best Answer
It was a bug in versions of
GNU Grep
earlier than this commit (i.e. earlier thanGNU
version2.5.3
).Quoting the relevant part of the changelog:
The initial commit that described the bug also added a test for it: