Hi, all When use --only-matching with -c, grep gives a counter-intuitive output on this input:
$ cat poc.txt alan cox is a guy. cox versus cox cox cool $ grep -o -c "cox" poc.txt What I suppose it to give is 5, but actually 3. I checked the manual and found "-c" reports the number of matching lines. Grep just behaves correctly :-) I also checked the POSIX docs and found that POSIX mandates the '-c' option , but not the '-o' option. So for the sake of compliance with the standard, it *does* have bias in favor of -c, and neglects the -o. However,I think what an utility behaves should not surprises its user. We should not blame user that why don't you read the f*cking manual first to make it work correctly. Naturally, counting the number of exact matching parts is a reasonable and quite useful feature. Since we shouldn't change the semantics of '-c". what should it do is just give a waring when user use -c and -o. and should clarify it in doc, just as what grep does when -C(context) and -o are used together. I don't know if this is a feasible way; and if it is , I will submit a patch for this. --- Larmbr Zhan