Subject: bug#70540: grep -c -r | grep -v ':0$'

From: "Dale R. Worley" <[email-masked]>
Date: Tue, 23 Apr 2024 14:32:38 -0400

At least once a week, and often several times a day, I want to search a
tree of files to list the files in a directory containing a pattern,
along with the *numbers* of patterns in the files.  Usually this is
because I'm looking for a file that contains a number of instances of
the pattern, from among which I will choose to copy something.  But
often the total number of files to be examined is large, and the total
number of matches in any file might also be large.

So "grep -r" is inconvenient, because it may return many more matches
than I want to examine, and it can be hard to see what all the
alternative files are among the large number of matches that can be
returned from any one file.

And "grep -c -r" is inconvenient, because it lists every file, even the
large number containing no match.

The idiom I usually use is "grep -c -r [pattern] [directory] | grep -v
':0$'", which lists the match counts, but only for files with non-zero
counts.

However, it seems "natural" to me that "grep -c -l", that is, "grep
--count --files-with-matches", should give me this result.  The current
(ver. 3.6) behavior of grep is that combination acts like
--files-with-matches alone.

Looking at the comments in the grep code, it seems that Posix specifies
that --count and --files-with-matches are incompatible, and thus this
change would be upward-compatible with Posix.

I've written a draft code revision, and it's simple, it doesn't require
changes to the overall code structure.

What do people think?

Dale