buggrep - Bugs: bug #33080, combination of --count (-c) and...


You are not allowed to post comments on this tracker with your current authentication level.

bug #33080: combination of --count (-c) and --only-matching (-o)

Submitter:  Seungbeom Kim <musiphil>
Submitted:  Thu 14 Apr 2011 11:32:25 PM UTC
Votes:  52  
Category:  None Severity:  3 - Normal
Item Group:  None Status:  Need Info
Privacy:  Public Assigned to:  None
Open/Closed:  Open

Wed 14 Mar 2018 06:50:20 AM UTC, comment #3: 

There is a corresponding bug in the Debian bug tracker since 2009:


FWIW, BSD grep seems to have the same bug.  Eventually it would be great if the requested behavior could be properly codified in POSIX.

era <era>
Sat 21 Sep 2013 05:54:10 AM UTC, comment #2: 

For this bug (#33080) it seems like -o would have to be taken into consideration before -c.
For bug #23635 Anonymous would like grep -C2;grep -C2 -v to output every line of the file. Currently it may include duplicates. This is similar and -C would have to be taken into consideration before -v.
If I'm not mistaken -C (as mentioned in #23635) and -o are gnu extensions but i haven't looked at the source and have no intention to. Only based off the results i assume gnu extensions are done last.
Here is another proposal for intuitive operation. Currently grep -ov will always return nothing which is not very useful. But what would be useful would be to return the nonmatching portion of the matching lines. This would give functionality, similar in many ways to #29827 but that is overcomplicated in my opinion. I am not prepared to write a patch at this time.
I hope you will understand and respond.

jikszzve <jikszzve>
Tue 28 Jun 2011 12:30:02 PM UTC, comment #1: 

I agree with the analysis, but an alternative is to just print an error.  Any other opinions?

Paolo Bonzini <bonzini>
Thu 14 Apr 2011 11:32:25 PM UTC, original submission:  

--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
true false true
$ grep --only-matching b test.txt

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

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
$ grep --count true test.txt
$ grep --only-matching true test.txt | wc -l
$ grep --only-matching --count true test.txt

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.

Seungbeom Kim <musiphil>


(Note: upload size limit is set to 16384 kB, after insertion of the required escape characters.)

No files currently attached


Depends on the following items: None found

Items that depend on this one: None found


Carbon-Copy List
  • -email is unavailable- added by era (Voted in favor of this item)
  • -email is unavailable- added by era (Posted a comment)
  • -email is unavailable- added by musiphil (Voted in favor of this item)
  • -email is unavailable- added by bonzini (Posted a comment)
  • -email is unavailable- added by musiphil (Submitted the item)

    There are 52 votes so far. Votes easily highlight which items people would like to see resolved in priority, independently of the priority of the item set by tracker managers.




    Follow 4 latest changes.

    Date Changed by Updated Field Previous Value => Replaced by
    2018-03-14 era Carbon-Copy- Added era
    2013-09-21 jikszzve Carbon-Copy- Added jikszzve
    2011-08-13 musiphil Carbon-Copy- Added musiphil
    2011-06-28 bonzini StatusNone Need Info

    Back to the top

    Powered by Savane 3.9