Subject: Re: bug#27931: grep -o fails to count empty lines (Debain Bug #532541)

From: Paul Eggert <[email-masked]>
Date: Thu, 3 Aug 2017 13:16:52 -0700

On 08/03/2017 06:28 AM, Santiago R.R. wrote:
> the -o option, which is supposed to return only the matching
> parts of the search, fails:

It's not failing. It's behaving as documented: -o outputs only nonempty 
matches. Otherwise, commands like 'grep -o "a*"' would output a separate 
line for each byte in the input. Although this behavior for -o is 
longstanding and is documented in the manual, it's not in the grep 
--help output so that's an oversight. I installed the attached to fix 
grep --help, and am closing the bug report on the GNU side.

Users who want to match empty lines can use 'grep "^$"', which is what 
I'd expect them to do anyway (-o would be superfluous there even if it 
included empty matches).

>From fe06a81c1fdaeda10bfdde82b43e2b18bfd1de5e Mon Sep 17 00:00:00 2001
From: Paul Eggert <[email-masked]>
Date: Thu, 3 Aug 2017 13:07:01 -0700
Subject: [PATCH] doc: improve -o help

* src/grep.c (usage): Document that -o outputs only nonempty
matches (Bug#27931).
---
 src/grep.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/src/grep.c b/src/grep.c
index 8d22aec..dd338d9 100644
--- a/src/grep.c
+++ b/src/grep.c
@@ -1949,7 +1949,7 @@ Output control:\n\
       --label=LABEL         use LABEL as the standard input file name prefix\n\
 "));
       printf (_("\
-  -o, --only-matching       show only the part of a line matching PATTERN\n\
+  -o, --only-matching       show only nonempty parts of lines matching PATTERN\n\
   -q, --quiet, --silent     suppress all normal output\n\
       --binary-files=TYPE   assume that binary files are TYPE;\n\
                             TYPE is 'binary', 'text', or 'without-match'\n\
-- 
2.13.3