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)
1/2/
for me on Debian Squeeze withGNU grep 2.6.3
. Maybe you should add some more specific details about your distro and grep version.GNU/Linux
withGNU grep 2.5.1
and showing result as @Acorn .GNU grep 2.5.4