]> git.ipfire.org Git - thirdparty/coreutils.git/commitdiff
csplit: fix regex suppression with specific match count
authorEmanuele Giacomelli <vpooldyn-linux@yahoo.it>
Sat, 8 Aug 2020 20:29:13 +0000 (21:29 +0100)
committerPádraig Brady <P@draigBrady.com>
Sat, 8 Aug 2020 20:50:13 +0000 (21:50 +0100)
* src/csplit.c (process_regexp): Process the line suppression
in all invocations so that the last match is suppressed.
Previously with a non infinite match count,
the last regex pattern was not suppressed.
* NEWS: Mention the bug fix.
* tests/misc/csplit-suppress-matched.pl: Add a test case.
Fixes https://bugs.gnu.org/42764

NEWS
src/csplit.c
tests/misc/csplit-suppress-matched.pl

diff --git a/NEWS b/NEWS
index 1881de1158cf186f3b67acc7559e582fb99d0c0b..61b711611e85af2ec4f80ec540a31e82024a9d39 100644 (file)
--- a/NEWS
+++ b/NEWS
@@ -9,6 +9,10 @@ GNU coreutils NEWS                                    -*- outline -*-
   is a non regular file.
   [bug introduced in coreutils-8.6]
 
+  csplit --suppress-matched now elides the last matched line
+  when a specific number of pattern matches are performed.
+  [bug introduced with the --suppress-matched feature in coreutils-8.22]
+
   du no longer crashes on XFS file systems when the directory hierarchy is
   heavily changed during the run.
   [bug introduced in coreutils-8.25]
index 9bd9c43b56a4068c31ff46c5c5f35cf3faf3c69f..93ff60dc624ccad2503406b08ae0023e4fd4f4c4 100644 (file)
@@ -803,9 +803,6 @@ process_regexp (struct control *p, uintmax_t repetition)
   if (!ignore)
     create_output_file ();
 
-  if (suppress_matched && current_line > 0)
-    remove_line ();
-
   /* If there is no offset for the regular expression, or
      it is positive, then it is not necessary to buffer the lines. */
 
@@ -893,6 +890,9 @@ process_regexp (struct control *p, uintmax_t repetition)
 
   if (p->offset > 0)
     current_line = break_line;
+
+  if (suppress_matched)
+    remove_line ();
 }
 
 /* Split the input file according to the control records we have built. */
index 80f5299d01a7e2bd18464973006ba648f8bcfa85..e15ebb0f27c2d3760ed48a1ad7dd61e4b8ec283d 100755 (executable)
@@ -67,21 +67,27 @@ my @csplit_tests =
     {OUTPUTS => [ "a\na\nYY\n", "\nXX\nb\nb\nYY\n","\nXX\nc\nYY\n",
                   "\nXX\nd\nd\nd\n" ] }],
 
-  # the newline (matched line) does not appears in the output files
+  # the newline (matched line) does not appear in the output files
   ["re-1", " --suppress-matched -q - '/^\$/' '{*}'", {IN_PIPE => $IN_UNIQ},
     {OUTPUTS => ["a\na\nYY\n", "XX\nb\nb\nYY\n", "XX\nc\nYY\n",
                  "XX\nd\nd\nd\n"]}],
 
-  # the 'XX' (matched line + offset 1) does not appears in the output files.
+  # the 'XX' (matched line + offset 1) does not appear in the output files.
   # the newline appears in the files (before each split, at the end of the file)
   ["re-2", "--suppress-matched -q - '/^\$/1' '{*}'", {IN_PIPE => $IN_UNIQ},
     {OUTPUTS => ["a\na\nYY\n\n","b\nb\nYY\n\n","c\nYY\n\n","d\nd\nd\n"]}],
 
-  # the 'YY' (matched line + offset of -1) does not appears in the output files
+  # the 'YY' (matched line + offset of -1) does not appear in the output files
   # the newline appears in the files (as the first line of the new split)
   ["re-3", " --suppress-matched -q - '/^\$/-1' '{*}'", {IN_PIPE => $IN_UNIQ},
     {OUTPUTS => ["a\na\n", "\nXX\nb\nb\n", "\nXX\nc\n", "\nXX\nd\nd\nd\n"]}],
 
+  # the last matched line for a non infinite match repetition is suppressed.
+  # Up to and including coreutils 8.32, the last match was output.
+  ["re-4", " --suppress-matched -q - '/^\$/' '{2}'", {IN_PIPE => $IN_UNIQ},
+    {OUTPUTS => ["a\na\nYY\n", "XX\nb\nb\nYY\n", "XX\nc\nYY\n",
+                 "XX\nd\nd\nd\n"]}],
+
   # Test two consecutive matched lines
   # without suppress-matched, the second file should contain a single newline.
   ["re-4.1", "-q - '/^\$/' '{*}'", {IN_PIPE => "a\n\n\nb\n"},