uniq (GNU coreutils) 9.7's man page and "--help" output says
(on Fedora 43):

|   -c, --count           prefix lines by the number of occurrences

The info documentation says:

| ‘-c’
| ‘--count’
|      Print the number of times each line occurred along with the line.

None document that the count is separated from the line by a
space, and that the count is space-left-padded to seven
characters (uniq.c: "%7jd "), i. e.:

| $ seq -f '0%f' 1 1 100 | cut -c -1 | uniq -c
|     100 0
| $ seq -f '0%f' 1 1 1000000 | cut -c -1 | uniq -c
| 1000000 0
| $ seq -f '0%f' 1 1 10000000 | cut -c -1 | uniq -c
| 10000000 0
| $

So it would be nice if that format could be documented so
that it could be relied upon.

While looking into this, I noticed that POSIX
(https://pubs.opengroup.org/onlinepubs/9799919799/utilities/uniq.html)
defines the format as:

| If the -c option is specified, the output file shall be empty or each line 
shall be of the form:

| "%d %s", <number of duplicates>, <line>

On the other hand, in the Examples section, it uses the
format "%5d %s":

| This first example tests the line counting option, comparing
| each line of the input file data starting from the second
| field:

| uniq -c -f 1 uniq_0I.t
|     1 #01 foo0 bar0 foo1 bar1
|     1 #02 bar0 foo1 bar1 foo1
|     1 #03 foo0 bar0 foo1 bar1
|     1 #04
|     2 #05 foo0 bar0 foo1 bar1
|     1 #07 bar0 foo1 bar1 foo0

Obviously, this offers the chance to make "uniq -c" depend
on POSIXLY_CORRECT or POSIXLY_EXAMPLES_CORRECT and other
such shenanigans.

But it would probably be more useful for POSIX to redefine
the format to:

| "%*d %s", <minimum field width>, <number of duplicates>, <line>

and declare minimum field width to be
implementation-defined, but at least 1 (or something
similarly sensible).



Reply via email to