Il 10/02/2013 00:28, Pollock, Wayne ha scritto: > In teaching REs, I use grep and recently noticed > the colors highlight all possible matches, not > the one match as per POSIX's rules: > > echo 'abcdefabc' | grep '[abc]' > > Should highlight only the first "a" (earliest match has > precedence, than longest). Instead, every "a", "b", and > "c" is highlighted. It would be my guess that grep historically > didn't need to know exactly what matched, just that something > did, so the code doesn't bother to apply any rules to determine > exactly what matched. > > I don't mind the current behavior as a default, but there should > be some option to force grep to only highlight the one match, > as per BRE/ERE/PCRE rules of precedence. >
We could add a --max-line-matches option. It would affect --color and -o. It would be mostly useful for -o, but would help you nicely as well. :) For your task, in the meanwhile you can use echo 'abcdefabc' | sed 's/[abc]/|&|/' Paolo
