Hi A couple of years ago [1] someone made a feature request for a wc option that would skip the total line when processing multiple files. I didn't see anyone commenting against it and it is something that I'm constantly hacking with `head -n-1`.
I've attached a patch that implements a new `--no-total` option to wc. I believe this patch to be trivial enough that I can't claim copyright for anything. Best regards David [1] https://lists.gnu.org/archive/html/coreutils/2015-11/msg00064.html
From 5ce5330a6758025834c442b3533e672dd02c9393 Mon Sep 17 00:00:00 2001 From: David Miguel Susano Pinto <carandraug+...@gmail.com> Date: Sun, 26 Jun 2022 00:27:06 +0100 Subject: [PATCH] wc: add --no-total option to not print final line with total counts * NEWS: reference the new option. * doc/coreutils.texi: document the new option * src/wc.c: add new --no-total option to not print a line with total counts when more than one file is specified. * tests/misc/wc-no-total.sh: new test suite for the new option. --- NEWS | 3 +++ doc/coreutils.texi | 5 +++++ src/wc.c | 14 ++++++++++++-- tests/misc/wc-no-total.sh | 33 +++++++++++++++++++++++++++++++++ 4 files changed, 53 insertions(+), 2 deletions(-) create mode 100755 tests/misc/wc-no-total.sh diff --git a/NEWS b/NEWS index a3a55541e..60e20d785 100644 --- a/NEWS +++ b/NEWS @@ -27,6 +27,9 @@ GNU coreutils NEWS -*- outline -*- factor now accepts the --exponents (-h) option to print factors in the form p^e, rather than repeating the prime p, e times. + wc now accepts the --no-total option to not print the total counts + when more than one file is specified. + * Noteworthy changes in release 9.1 (2022-04-15) [stable] diff --git a/doc/coreutils.texi b/doc/coreutils.texi index 7bca37b71..62f73f39b 100644 --- a/doc/coreutils.texi +++ b/doc/coreutils.texi @@ -3839,6 +3839,11 @@ Tabs are set at every 8th column. Display widths of wide characters are considered. Non-printable characters are given 0 width. +@item --no-total +@opindex --no-total +Do not print the final line with the cumulative counts, which is +printed by default when more than one @var{file} is given. + @macro filesZeroFromOption{cmd,withTotalOption,subListOutput} @item --files0-from=@var{file} @opindex --files0-from=@var{file} diff --git a/src/wc.c b/src/wc.c index dee8233a6..13565b11a 100644 --- a/src/wc.c +++ b/src/wc.c @@ -81,7 +81,7 @@ static uintmax_t max_line_length; /* Which counts to print. */ static bool print_lines, print_words, print_chars, print_bytes; -static bool print_linelength; +static bool print_linelength, print_total; /* The print width of each count. */ static int number_width; @@ -112,6 +112,7 @@ enum { DEBUG_PROGRAM_OPTION = CHAR_MAX + 1, FILES0_FROM_OPTION, + NO_TOTAL_OPTION, }; static struct option const longopts[] = @@ -123,6 +124,7 @@ static struct option const longopts[] = {"debug", no_argument, NULL, DEBUG_PROGRAM_OPTION}, {"files0-from", required_argument, NULL, FILES0_FROM_OPTION}, {"max-line-length", no_argument, NULL, 'L'}, + {"no-total", no_argument, NULL, NO_TOTAL_OPTION}, {GETOPT_HELP_OPTION_DECL}, {GETOPT_VERSION_OPTION_DECL}, {NULL, 0, NULL, 0} @@ -215,6 +217,9 @@ the following order: newline, word, character, byte, maximum line length.\n\ If F is - then read names from standard input\n\ -L, --max-line-length print the maximum display width\n\ -w, --words print the word counts\n\ +"), stdout); + fputs (_("\ + --no-total do not print the final line with total counts\n\ "), stdout); fputs (HELP_OPTION_DESCRIPTION, stdout); fputs (VERSION_OPTION_DESCRIPTION, stdout); @@ -807,6 +812,7 @@ main (int argc, char **argv) print_lines = print_words = print_chars = print_bytes = false; print_linelength = false; + print_total = true; total_lines = total_words = total_chars = total_bytes = max_line_length = 0; while ((optc = getopt_long (argc, argv, "clLmw", longopts, NULL)) != -1) @@ -840,6 +846,10 @@ main (int argc, char **argv) files_from = optarg; break; + case NO_TOTAL_OPTION: + print_total = false; + break; + case_GETOPT_HELP_CHAR; case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS); @@ -987,7 +997,7 @@ main (int argc, char **argv) if (read_tokens) readtokens0_free (&tok); - if (1 < argv_iter_n_args (ai)) + if (1 < argv_iter_n_args (ai) && print_total) write_counts (total_lines, total_words, total_chars, total_bytes, max_line_length, _("total")); diff --git a/tests/misc/wc-no-total.sh b/tests/misc/wc-no-total.sh new file mode 100755 index 000000000..cda4b3f06 --- /dev/null +++ b/tests/misc/wc-no-total.sh @@ -0,0 +1,33 @@ +#!/bin/sh +# Show that wc's --no-total option works. + +# Copyright (C) 2006-2022 Free Software Foundation, Inc. + +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. + +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. + +# You should have received a copy of the GNU General Public License +# along with this program. If not, see <https://www.gnu.org/licenses/>. + +. "${srcdir=.}/tests/init.sh"; path_prepend_ ./src +print_ver_ wc + +echo 2 > 2b || framework_failure_ +echo 2 words > 2w || framework_failure_ + +wc --no-total 2b 2w > out || fail=1 +cat <<\EOF > exp || framework_failure_ + 1 1 2 2b + 1 2 8 2w +EOF + +compare exp out || fail=1 + +Exit $fail -- 2.20.1