Subject: Re: bug#16871: problems about matching newline (with -z)

From: Paul Eggert <[email-masked]>
Date: Thu, 24 Apr 2014 21:27:38 -0700

Stephane Chazelas wrote:
> The doc has a confusing statement ... Same confusion in tests/pcre:

Thanks, I installed the attached patch to fix those.

> We can match a newline with grep -zP 'a\nb' (or '\x0a' or '\012'
> or '[\n]'...) but not easily without -P. Same for NUL
> characters.

Yes, that's a downside of the POSIX notation, and it'd be nice to extend 
POSIX to allow easy matching for newlines and/or null bytes.  I'll mark 
this bug report as a wishlist bug.

From 2fd9ce3bcf55ec80aa5b1d3775fe00ccb078d7dd Mon Sep 17 00:00:00 2001
From: Paul Eggert <[email-masked]>
Date: Thu, 24 Apr 2014 21:24:22 -0700
Subject: [PATCH] misc: fix doc and test bugs re grep -z

Problem reported by Stephane Chazelas in: http://bugs.gnu.org/16871
* doc/grep.texi (Usage): Remove incorrect example with -P.
* tests/pcre: Improve test so that it actually tests whether \s
matches a newline.
---
 doc/grep.texi | 16 +++++-----------
 tests/pcre    |  4 ++--
 2 files changed, 7 insertions(+), 13 deletions(-)

diff --git a/doc/grep.texi b/doc/grep.texi
index f631f03..59d0d3c 100644
--- a/doc/grep.texi
+++ b/doc/grep.texi
@@ -1719,25 +1719,19 @@ How can I match across lines?
 
 Standard grep cannot do this, as it is fundamentally line-based.
 Therefore, merely using the @code{[:space:]} character class does not
-match newlines in the way you might expect.  However, if your grep is
-compiled with Perl patterns enabled, the Perl @samp{s}
-modifier (which makes @code{.} match newlines) can be used:
-
-@example
-printf 'foo\nbar\n' | grep -P '(?s)foo.*?bar'
-@end example
+match newlines in the way you might expect.
 
 With the GNU @command{grep} option @code{-z} (@pxref{File and
 Directory Selection}), the input is terminated by null bytes.  Thus,
-you can match newlines in the input, but the output will be the whole
-file, so this is really only useful to determine if the pattern is
-present:
+you can match newlines in the input, but typically if there is a match
+the entire input is output, so this usage is often combined with
+output-suppressing options like @option{-q}, e.g.:
 
 @example
 printf 'foo\nbar\n' | grep -z -q 'foo[[:space:]]\+bar'
 @end example
 
-Failing either of those options, you need to transform the input
+If this does not suffice, you can transform the input
 before giving it to @command{grep}, or turn to @command{awk},
 @command{sed}, @command{perl}, or many other utilities that are
 designed to operate across lines.
diff --git a/tests/pcre b/tests/pcre
index cbe6884..7efa560 100755
--- a/tests/pcre
+++ b/tests/pcre
@@ -1,5 +1,5 @@
 #! /bin/sh
-# Ensure that with -P, \s*$ matches a newline.
+# Ensure that with -P, \s matches a newline.
 #
 # Copyright (C) 2001, 2006, 2009-2014 Free Software Foundation, Inc.
 #
@@ -12,7 +12,7 @@ require_pcre_
 
 fail=0
 
-# See CVS revision 1.32 of "src/search.c".
 echo | grep -P '\s*$' || fail=1
+echo | grep -zP '\s$' || fail=1
 
 Exit $fail
-- 
1.9.0