[Pcre-svn] [572] code/trunk: Add -t to pcre2grep.

Top Page
Delete this message
Author: Subversion repository
Date:  
To: pcre-svn
Subject: [Pcre-svn] [572] code/trunk: Add -t to pcre2grep.
Revision: 572
          http://www.exim.org/viewvc/pcre2?view=rev&revision=572
Author:   ph10
Date:     2016-10-16 17:48:14 +0100 (Sun, 16 Oct 2016)
Log Message:
-----------
Add -t to pcre2grep.


Modified Paths:
--------------
    code/trunk/ChangeLog
    code/trunk/RunGrepTest
    code/trunk/doc/pcre2grep.1
    code/trunk/src/pcre2grep.c
    code/trunk/testdata/grepoutput


Modified: code/trunk/ChangeLog
===================================================================
--- code/trunk/ChangeLog    2016-10-15 10:28:27 UTC (rev 571)
+++ code/trunk/ChangeLog    2016-10-16 16:48:14 UTC (rev 572)
@@ -78,7 +78,9 @@
 (untested by me). Also, look for GREP_COLOUR or GREP_COLOR if the environment 
 variables PCRE2GREP_COLOUR and PCRE2GREP_COLOR are not found.


+14. Add the -t (grand total) option to pcre2grep.

+
Version 10.22 29-July-2016
--------------------------


Modified: code/trunk/RunGrepTest
===================================================================
--- code/trunk/RunGrepTest    2016-10-15 10:28:27 UTC (rev 571)
+++ code/trunk/RunGrepTest    2016-10-16 16:48:14 UTC (rev 572)
@@ -568,6 +568,30 @@
 (cd $srcdir; $valgrind $vjs $pcre2grep --file-offsets -M 'match (\d+):\n (.)\n' testdata/grepinput) >>testtrygrep
 echo "RC=$?" >>testtrygrep


+echo "---------------------------- Test 113 -----------------------------" >>testtrygrep
+(cd $srcdir; $valgrind $vjs $pcre2grep --total-count 'the' testdata/grepinput*) >>testtrygrep
+echo "RC=$?" >>testtrygrep
+
+echo "---------------------------- Test 114 -----------------------------" >>testtrygrep
+(cd $srcdir; $valgrind $vjs $pcre2grep -tc 'the' testdata/grepinput*) >>testtrygrep
+echo "RC=$?" >>testtrygrep
+
+echo "---------------------------- Test 115 -----------------------------" >>testtrygrep
+(cd $srcdir; $valgrind $vjs $pcre2grep -tlc 'the' testdata/grepinput*) >>testtrygrep
+echo "RC=$?" >>testtrygrep
+
+echo "---------------------------- Test 116 -----------------------------" >>testtrygrep
+(cd $srcdir; $valgrind $vjs $pcre2grep -th 'the' testdata/grepinput*) >>testtrygrep
+echo "RC=$?" >>testtrygrep
+
+echo "---------------------------- Test 117 -----------------------------" >>testtrygrep
+(cd $srcdir; $valgrind $vjs $pcre2grep -tch 'the' testdata/grepinput*) >>testtrygrep
+echo "RC=$?" >>testtrygrep
+
+echo "---------------------------- Test 118 -----------------------------" >>testtrygrep
+(cd $srcdir; $valgrind $vjs $pcre2grep -tL 'the' testdata/grepinput*) >>testtrygrep
+echo "RC=$?" >>testtrygrep
+
# Now compare the results.

$cf $srcdir/testdata/grepoutput testtrygrep

Modified: code/trunk/doc/pcre2grep.1
===================================================================
--- code/trunk/doc/pcre2grep.1    2016-10-15 10:28:27 UTC (rev 571)
+++ code/trunk/doc/pcre2grep.1    2016-10-16 16:48:14 UTC (rev 572)
@@ -1,4 +1,4 @@
-.TH PCRE2GREP 1 "14 October 2016" "PCRE2 10.23"
+.TH PCRE2GREP 1 "16 October 2016" "PCRE2 10.23"
 .SH NAME
 pcre2grep - a grep with Perl-compatible regular expressions.
 .SH SYNOPSIS
@@ -180,13 +180,15 @@
 .TP
 \fB-c\fP, \fB--count\fP
 Do not output lines from the files that are being scanned; instead output the
-number of matches (or non-matches if \fB-v\fP is used) that would otherwise
-have caused lines to be shown. By default, this count is the same as the number
-of suppressed lines, but if the \fB-M\fP (multiline) option is used (without
-\fB-v\fP), there may be more suppressed lines than the number of matches.
+number of lines that would have been shown, either because they matched, or, if
+\fB-v\fP is set, because they failed to match. By default, this count is
+exactly the same as the number of suppressed lines, but if the \fB-M\fP
+(multiline) option is used (without \fB-v\fP), there may be more suppressed
+lines than the number of matches.
 .sp
 If no lines are selected, the number zero is output. If several files are are
-being scanned, a count is output for each of them. However, if the
+being scanned, a count is output for each of them and the \fB-t\fP option can 
+be used to cause a total to be output at the end. However, if the
 \fB--files-with-matches\fP option is also used, only those files whose counts
 are greater than zero are listed. When \fB-c\fP is used, the \fB-A\fP,
 \fB-B\fP, and \fB-C\fP options are ignored.
@@ -573,6 +575,17 @@
 quietly skipped. However, the return code is still 2, even if matches were
 found in other files.
 .TP
+\fB-t\fP, \fB--total-count\fP
+This option is useful when scanning more than one file. If used on its own,
+\fB-t\fP suppresses all output except for a grand total number of matching
+lines (or non-matching lines if \fB-v\fP is used) in all the files. If \fB-t\fP
+is used with \fB-c\fP, a grand total is output except when the previous output
+is just one line. In other words, it is not output when just one file's count
+is listed. If file names are being output, the grand total is preceded by
+"TOTAL:". Otherwise, it appears as just another number. The \fB-t\fP option is
+ignored when used with \fB-L\fP (list files without matches), because the grand
+total would always be zero.
+.TP
 \fB-u\fP, \fB--utf-8\fP
 Operate in UTF-8 mode. This option is available only if PCRE2 has been compiled
 with UTF-8 support. All patterns (including those for any \fB--exclude\fP and
@@ -778,6 +791,6 @@
 .rs
 .sp
 .nf
-Last updated: 14 October 2016
+Last updated: 16 October 2016
 Copyright (c) 1997-2016 University of Cambridge.
 .fi


Modified: code/trunk/src/pcre2grep.c
===================================================================
--- code/trunk/src/pcre2grep.c    2016-10-15 10:28:27 UTC (rev 571)
+++ code/trunk/src/pcre2grep.c    2016-10-16 16:48:14 UTC (rev 572)
@@ -177,6 +177,8 @@
 static int max_bufthird = PCRE2GREP_MAX_BUFSIZE;
 static int bufsize = 3*PCRE2GREP_BUFSIZE;
 static int endlinetype;
+static int total_count = 0;
+static int counts_printed = 0;


#if defined HAVE_WINDOWS_H && HAVE_WINDOWS_H
static int dee_action = dee_SKIP;
@@ -218,6 +220,7 @@
static BOOL resource_error = FALSE;
static BOOL quiet = FALSE;
static BOOL show_only_matching = FALSE;
+static BOOL show_total_count = FALSE;
static BOOL silent = FALSE;
static BOOL utf = FALSE;

@@ -402,6 +405,7 @@
   { OP_OP_NUMBER, 'S',      &S_arg,            "jeffS",         "replace matched (sub)string with X" },
 #endif
   { OP_NODATA,    's',      NULL,              "no-messages",   "suppress error messages" },
+  { OP_NODATA,    't',      NULL,              "total-count",   "print total count of matching lines" },
   { OP_NODATA,    'u',      NULL,              "utf",           "use UTF mode" },
   { OP_NODATA,    'V',      NULL,              "version",       "print version information and exit" },
   { OP_NODATA,    'v',      NULL,              "invert-match",  "select non-matching lines" },
@@ -2115,7 +2119,7 @@


     /* Just count if just counting is wanted. */


-    else if (count_only) count++;
+    else if (count_only || show_total_count) count++;


     /* When handling a binary file and binary-files==binary, the "binary"
     variable will be set true (it's false in all other cases). In this
@@ -2458,7 +2462,7 @@
 /* End of file; print final "after" lines if wanted; do_after_lines sets
 hyphenpending if it prints something. */


-if (!show_only_matching && !count_only)
+if (!show_only_matching && !(count_only|show_total_count))
   {
   do_after_lines(lastmatchnumber, lastmatchrestart, endptr, printname);
   hyphenpending |= endhyphenpending;
@@ -2482,9 +2486,11 @@
     if (printname != NULL && filenames != FN_NONE)
       fprintf(stdout, "%s:", printname);
     fprintf(stdout, "%d" STDOUT_NL, count);
+    counts_printed++; 
     }
   }


+total_count += count; /* Can be set without count_only */
return rc;
}

@@ -2793,6 +2799,7 @@
   case 'q': quiet = TRUE; break;
   case 'r': dee_action = dee_RECURSE; break;
   case 's': silent = TRUE; break;
+  case 't': show_total_count = TRUE; break; 
   case 'u': options |= PCRE2_UTF; utf = TRUE; break;
   case 'v': invert = TRUE; break;
   case 'w': process_options |= PO_WORD_MATCH; break;
@@ -3688,7 +3695,17 @@
   if (frc > 1) rc = frc;
     else if (frc == 0 && rc == 1) rc = 0;
   }
+  
+/* Show the total number of matches if requested, but not if only one file's 
+count was printed. */


+if (show_total_count && counts_printed != 1 && filenames != FN_NOMATCH_ONLY)
+  {
+  if (counts_printed != 0 && filenames >= FN_DEFAULT) 
+    fprintf(stdout, "TOTAL:");
+  fprintf(stdout, "%d" STDOUT_NL, total_count);
+  } 
+
 EXIT:
 #ifdef SUPPORT_PCRE2GREP_JIT
 if (jit_stack != NULL) pcre2_jit_stack_free(jit_stack);


Modified: code/trunk/testdata/grepoutput
===================================================================
(Binary files differ)