URL:
<http://savannah.gnu.org/bugs/?33080>
Summary: combination of --count (-c) and --only-matching (-o)
Project: grep
Submitted by: musiphil
Submitted on: Thu 14 Apr 2011 04:32:25 PM PDT
Category: None
Severity: 3 - Normal
Item Group: None
Status: None
Privacy: Public
Assigned to: None
Open/Closed: Open
Discussion Lock: Any
_______________________________________________________
Details:
--only-matching (-o) causes grep to print all the matching parts separately,
no matter how many times such parts occur on a single line. For example:
$ cat > test.txt
true true false
false true
false
true false true
$ grep --only-matching b test.txt
true
true
true
true
true
If you want to count how many matches you have, you naturally think you can
use --count (-c). But you can't:
$ grep --only-matching --count true test.txt
3
You still get the number of "lines" that match the pattern, not how many
"matches". The --only-matching option is effectively ignored, in a silent
way.
This is counter-intuitive, when you consider that 'grep --count ...' is
generally equivalent to 'grep ... | wc -l', which doesn't hold if
--only-matching is used.
$ grep true test.txt | wc -l
3
$ grep --count true test.txt
3
$ grep --only-matching true test.txt | wc -l
5
$ grep --only-matching --count true test.txt
3
I suggest that grep --count should print the number of lines that would be
printed without --count, whether or not --only-matching is used; that is, the
number of lines that match the pattern without --only-matching, and the number
of matches with --only-matching.
_______________________________________________________
Reply to this item at:
<http://savannah.gnu.org/bugs/?33080>
_______________________________________________
Message sent via/by Savannah
http://savannah.gnu.org/