patchgrep - Patches: patch #7577, `grep -lc' to show only matching...


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

patch #7577: `grep -lc' to show only matching files

Submitted by:  Santiago Ruano Rincón <santiago>
Submitted on:  Wed 13 Jul 2011 11:56:25 PM UTC  
Category:  None Priority:  5 - Normal
Status:  In Progress Privacy:  Public
Assigned to:  None Open/Closed:  Open

Wed 10 Aug 2011 07:17:37 PM UTC, comment #4: 

Two people filled different bugs in debian asking for the same behavior from grep -lc, and, with my user's hat put on, I found the combination of --count and --files-with-matches clear. grep's man page doesn't say that those options only print matching count or file' names, as grep --help tries to say. Man page either says that those control output options cannot be used together (Maybe it should?).

However, in the name of standards, I'd prefer '-c --min-count' to achieve that behavior. So I agree with you, and this patch shouldn't be considered.


Santiago Ruano Rincón <santiago>
Thu 14 Jul 2011 09:07:52 AM UTC, comment #3: 

I'm inclined of including instead, with a -M command line so that you can achieve the result with grep -cM1.  This has the advantage that the interaction with -v is clearer, too.

At the same time, -lc and -Lc could give an error.

Paolo Bonzini <bonzini>
Thu 14 Jul 2011 09:05:06 AM UTC, comment #2: 

I don't understand what you mean by "garbled output from both options".  "grep -lc" here gives exactly the same output as "grep -l".  I understand why the patch is nice to have, but personally I would never have thought of specifying them together. :)

Also the help shows they are contradicting:

-l, --files-with-matches only print FILE names containing matches
-c, --count only print a count of matching lines per FILE

(emphasis on "only" added).  Right now, my acceptance of the patch is conditioned on tweaking the help text or documentation to make the expected behavior clear.

Paolo Bonzini <bonzini>
Thu 14 Jul 2011 12:00:50 AM UTC, comment #1: 

According to POSIX,
-c and -l cannot portably be used together.  But we could certainly support it as a GNU extension.  Did you have a patch already in mind?

Eric Blake <ericb>
Project Member
Wed 13 Jul 2011 11:56:25 PM UTC, original submission:  

Peter Samuelson has submitted a patch to Debian to modify the behavior of `grep -lc'. Copying what he wrote in


Okay, grep has two very useful options, -l and -c:

  -l, --files-with-matches  only print FILE names containing matches
  -c, --count               only print a count of matching lines per FILE

It seems only logical that 'grep -lc' would count the matching lines per file, but skip files with no matches.  Obvious, right? (: But the status quo is that 'grep -lc' gives garbled output from both options, i.e. "undefined behavior".

Fortunately, it's a simple change.  Would seven lines of source bloat be considered unacceptible?  Note that this change isn't just for theoretical consistency -- I find it very practical.  Because otherwise I am forever saying

  grep -c ..... | grep -v :0


I think his patch is correct and useful. Could you please consider this patch?



Santiago Ruano Rincón <santiago>


Attached Files
file #23650:  grep-lc.patch added by santiago (1KiB - text/x-diff)


Depends on the following items: None found

Items that depend on this one: None found


Carbon-Copy List
  • -email is unavailable- added by bonzini (Posted a comment)
  • -email is unavailable- added by ericb (Posted a comment)
  • -email is unavailable- added by santiago (Submitted the item)

    Do you think this task is very important?
    If so, you can add your encouragement to it.
    This task has 0 encouragements so far.

    Only logged-in users can vote.




    Follow 2 latest changes.

    Date Changed by Updated Field Previous Value => Replaced by
    2011-07-14 bonzini StatusNone => In Progress
    2011-07-13 santiago Attached File- => Added grep-lc.patch, #23650

    Back to the top

    Powered by Savane 3.5