tech-userlevel archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

uniq -cdu



Our uniq(1) does not allow combining the -c mode with -d or with -u.
This is irritating, because uniq -dc is often useful behavior and
working around the restriction is a nuisance.

Meanwhile, while it does allow using -d and -u together, doing so
causes it to print all lines of the input, while from reading the man
page or the standard one would expect it to print nothing.

Furthermore, the uniq in GNU coreutils does not exhibit either of
these behaviors; it allows uniq -dc (and uniq -du) and prints nothing
if -du is given.

While the SYNOPSIS line in at least one version of the standard:

   http://www.opengroup.org/onlinepubs/009695399/utilities/uniq.html

describes these three options as mutually exclusive, but even if that
is intended to be normative it can't reasonably be construed as a
prohibition on allowing combinations of them... and we already allow
one of the combinations anyway.

Unless anyone can think of a persuasive reason not to I'm going to
fix this with the following patch:

(note that the test of *str that I removed is always true)


Index: uniq.1
===================================================================
RCS file: /cvsroot/src/usr.bin/uniq/uniq.1,v
retrieving revision 1.11
diff -u -p -r1.11 uniq.1
--- uniq.1      6 Jan 2007 02:18:24 -0000       1.11
+++ uniq.1      30 Aug 2010 01:26:54 -0000
@@ -40,7 +40,7 @@
 .Nd report or filter out repeated lines in a file
 .Sh SYNOPSIS
 .Nm
-.Op Fl c | Fl d | Fl u
+.Op Fl cdu
 .Op Fl f Ar fields
 .Op Fl s Ar chars
 .Oo
Index: uniq.c
===================================================================
RCS file: /cvsroot/src/usr.bin/uniq/uniq.c,v
retrieving revision 1.15
diff -u -p -r1.15 uniq.c
--- uniq.c      21 Jul 2008 14:19:27 -0000      1.15
+++ uniq.c      30 Aug 2010 01:26:54 -0000
@@ -107,13 +107,6 @@ main (int argc, char *argv[])
 done:  argc -= optind;
        argv +=optind;
 
-       /* If no flags are set, default is -d -u. */
-       if (cflag) {
-               if (dflag || uflag)
-                       usage();
-       } else if (!dflag && !uflag)
-               dflag = uflag = 1;
-
        switch(argc) {
        case 0:
                ifp = stdin;
@@ -192,10 +185,13 @@ static void
 show(FILE *ofp, const char *str)
 {
 
-       if (cflag && *str)
+       if ((dflag && repeats == 0) || (uflag && repeats > 0))
+               return;
+       if (cflag) {
                (void)fprintf(ofp, "%4d %s", repeats + 1, str);
-       if ((dflag && repeats) || (uflag && !repeats))
+       } else {
                (void)fprintf(ofp, "%s", str);
+       }
 }
 
 static const char *


-- 
David A. Holland
dholland%netbsd.org@localhost


Home | Main Index | Thread Index | Old Index