Subject: bug#24024: closed (Re: bug#24024: grep: Mixing "max-count" and "after-context" outputs too few lines)

From: [email-masked] (GNU bug Tracking System)
Date: Thu, 08 Sep 2016 05:26:02 +0000

Your bug report

#24024: grep: Mixing "max-count" and "after-context" outputs too few lines

which was filed against the grep package, has been closed.

The explanation is attached below, along with your original report.
If you require more details, please reply to [email-masked].

-- 
24024: http://debbugs.gnu.org/cgi/bugreport.cgi?bug=24024
GNU Bug Tracking System
Contact [email-masked] with problems

Subject: Re: bug#24024: grep: Mixing "max-count" and "after-context" outputs too few lines
From: Paul Eggert <[email-masked]>
To: Santiago Ruano Rincón <[email-masked]>
Cc: Norihiro Tanaka <[email-masked]>, [email-masked], [email-masked], [email-masked]
Date: Wed, 7 Sep 2016 22:25:03 -0700
List-Id: <debbugs-submit.debbugs.gnu.org>

>> Given after-context=3 it is expected to output at least 4 lines
>> as documented, but adding max-count=1 makes it stop on the next
>> matching line.

Thanks for reporting this. Although grep's behavior is documented ("context does 
not include matching lines" in the node General Output Control) the 
documentation could be clearer and I installed the attached patch.
From 90a2dd8b7f93ef0a8f08741e6fcb07220f9549f6 Mon Sep 17 00:00:00 2001
From: Paul Eggert <[email-masked]>
Date: Wed, 7 Sep 2016 22:22:37 -0700
Subject: [PATCH] doc: define "context lines"
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Reported by Igor Bogomazov via Santiago Ruano Rincón (Bug#24024).
* doc/grep.texi (Context Line Control): Define "context lines".
---
 doc/grep.texi | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)

diff --git a/doc/grep.texi b/doc/grep.texi
index 80768dd..7e51d45 100644
--- a/doc/grep.texi
+++ b/doc/grep.texi
@@ -338,6 +338,7 @@ do
 done
 @end example
 
+@cindex context lines
 When @command{grep} stops after @var{num} matching lines,
 it outputs any trailing context lines.
 Since context does not include matching lines,
@@ -501,8 +502,11 @@ even those that contain newline characters.
 @node Context Line Control
 @subsection Context Line Control
 
+@cindex context lines
+@dfn{Context lines} are non-matching lines that are near a matching line.
+They are output only if one of the following options are used.
 Regardless of how these options are set,
-@command{grep} will never print any given line more than once.
+@command{grep} never outputs any given line more than once.
 If the @option{-o} (@option{--only-matching}) option is specified,
 these options have no effect and a warning is given upon their use.
 
@@ -530,7 +534,7 @@ Print @var{num} lines of leading context before matching lines.
 @opindex -C
 @opindex --context
 @opindex -@var{num}
-@cindex context
+@cindex context lines
 Print @var{num} lines of leading and trailing output context.
 
 @item --group-separator=@var{string}
-- 
2.7.4

Subject: grep: Mixing "max-count" and "after-context" outputs too few lines
From: Santiago Ruano Rincón <[email-masked]>
To: Igor Bogomazov <[email-masked]>, [email-masked], bug-grep <[email-masked]>
Date: Tue, 19 Jul 2016 13:53:45 +0200
List-Id: <debbugs-submit.debbugs.gnu.org>

Hi,

Igor Bogomazov <[email-masked]> filed the following bug against grep in
Debian.

El 18/07/16 a las 14:30, Igor Bogomazov escribió:
> Package: grep
> Version: 2.25-6
> Severity: normal
> 
> 
> Given after-context=3 it is expected to output at least 4 lines
> as documented, but adding max-count=1 makes it stop on the next
> matching line.
> 
> What I do:
> 
> $ printf '%s\n' a b a c | grep -m1 -A3 a
> 
> What I see:
> 
> a
> b
> 
> What I expect:
> 
> a
> b
> a
> c
> 
> -- System Information:
> Debian Release: stretch/sid
>   APT prefers unstable
>   APT policy: (500, 'unstable'), (500, 'testing'), (500, 'stable')
> Architecture: amd64 (x86_64)
> 
> Kernel: Linux 4.6.0-1-amd64 (SMP w/4 CPU cores)
> Locale: LANG=en_US.UTF-8, LC_CTYPE=en_US.UTF-8 (charmap=UTF-8)
> Shell: /bin/sh linked to /bin/dash
> Init: systemd (via /run/systemd/system)
> 
> Versions of packages grep depends on:
> ii  dpkg          1.18.9
> ii  install-info  6.1.0.dfsg.1-8
> ii  libc6         2.23-1
> ii  libpcre3      2:8.38-3.1
> 
> grep recommends no packages.
> 
> Versions of packages grep suggests:
> ii  libpcre3  2:8.38-3.1
> 
> -- no debconf information
> 

Note also the behavior when there is not a second match:

% printf '%s\n' a b d c | grep -m1 -A3 a
a
b
d
c

Cheers,

Santiago