>From 6b43c1d0ea6faa460f77555eeeb74e0523e2f3a0 Mon Sep 17 00:00:00 2001 From: Roni Kallio Date: Thu, 8 Oct 2020 01:33:38 +0300 Subject: [PATCH] uniq: --count-separator option Add a count-separator option, that allows us to set the separator used between occurrences and line content, when the -c option is enabled. --- src/uniq.c | 32 ++++++++++++++++++++++++++++++-- 1 file changed, 30 insertions(+), 2 deletions(-) diff --git a/src/uniq.c b/src/uniq.c index e0247579b..2e9d83e9d 100644 --- a/src/uniq.c +++ b/src/uniq.c @@ -79,6 +79,10 @@ static bool output_later_repeated; /* If true, ignore case when comparing. */ static bool ignore_case; +/* Separator character used between output lines and the number of times they + occur in the input. */ +static char count_separator = ' '; + enum delimit_method { /* No delimiters output. --all-repeated[=none] */ @@ -136,12 +140,14 @@ static enum grouping_method grouping = GM_NONE; enum { - GROUP_OPTION = CHAR_MAX + 1 + GROUP_OPTION = CHAR_MAX + 1, + COUNT_SEPARATOR_OPTION }; static struct option const longopts[] = { {"count", no_argument, NULL, 'c'}, + {"count-separator", required_argument, NULL, COUNT_SEPARATOR_OPTION}, {"repeated", no_argument, NULL, 'd'}, {"all-repeated", optional_argument, NULL, 'D'}, {"group", optional_argument, NULL, GROUP_OPTION}, @@ -178,6 +184,9 @@ With no options, matching lines are merged to the first occurrence.\n\ fputs (_("\ -c, --count prefix lines by the number of occurrences\n\ + --count-separator=CHAR separator character used between output lines\n\ + and the number of times they occur in the\n\ + input \n\ -d, --repeated only print duplicate lines, one for each group\n\ "), stdout); fputs (_("\ @@ -308,7 +317,7 @@ writeline (struct linebuffer const *line, return; if (countmode == count_occurrences) - printf ("%7" PRIuMAX " ", linecount + 1); + printf ("%7" PRIuMAX "%c", linecount + 1, count_separator); fwrite (line->buffer, sizeof (char), line->length, stdout); } @@ -483,6 +492,7 @@ main (int argc, char **argv) char const *file[2]; char delimiter = '\n'; /* change with --zero-terminated, -z */ bool output_option_used = false; /* if true, one of -u/-d/-D/-c was used */ + bool count_separator_used = false; /* true if --count-separator was used */ file[0] = file[1] = "-"; initialize_main (&argc, &argv); @@ -594,6 +604,18 @@ main (int argc, char **argv) grouping_method_map); break; + case COUNT_SEPARATOR_OPTION: + /* Separator for count and line. Interpret --count-separator='' to + mean 'use NUL byte as * delimiter.' */ + if (optarg[0] != '\0' && optarg[1] != '\0') + { + error (0, 0, _("The separator must be a single character")); + usage (EXIT_FAILURE); + } + count_separator = optarg[0]; + count_separator_used = true; + break; + case 'f': skip_field_option_type = SFO_NEW; skip_fields = size_opt (optarg, @@ -656,6 +678,12 @@ main (int argc, char **argv) usage (EXIT_FAILURE); } + if (count_separator_used && countmode == count_none) + { + error (0, 0, _("--count-separator requires -c")); + usage (EXIT_FAILURE); + } + check_file (file[0], file[1], delimiter); return EXIT_SUCCESS; -- 2.26.2