Skip site navigation (1)Skip section navigation (2)
Date:      Fri, 4 Oct 2002 17:58:06 -0400
From:      Mikhail Teterin <mi+mx@aldan.algebra.com>
To:        FreeBSD-gnats-submit@FreeBSD.org
Cc:        tjr@FreeBSD.org
Subject:   bin/43675: uniq prints last, not first of the identical lines [patch]
Message-ID:  <200210041758.06299.mi%2Bmx@aldan.algebra.com>

next in thread | raw e-mail | index | archive | help

>Number:         43675
>Category:       bin
>Synopsis:       uniq prints last, not first of the identical lines [patch]
>Confidential:   no
>Severity:       non-critical
>Priority:       low
>Responsible:    freebsd-bugs
>State:          open
>Quarter:        
>Keywords:       
>Date-Required:
>Class:          change-request
>Submitter-Id:   current-users
>Arrival-Date:   Fri Oct 04 15:00:18 PDT 2002
>Closed-Date:
>Last-Modified:
>Originator:     Mikhail Teterin
>Release:        FreeBSD 5.0-CURRENT i386
>Organization:
Virtual Estates, Inc.
>Environment:
System: FreeBSD misha.murex.com 5.0-CURRENT FreeBSD 5.0-CURRENT #0: Fri Oct 4 
14:43:49 EDT 2002 root@misha.murex.com:/misha/obj/misha/src/sys/Misha-g i386


>Description:

        Presently, uniq(1) prints the last instead of the first of the
        sequence of identical lines. This does not make much difference
        in many cases, but in something like

                tail -f [some log].log | uniq

        it is awkward -- uniq will not output the line until it sees the
        different one (or until the input is closed).

        The included patch modifies uniq to output the first line,
        whenever possible (it is not, for example, when the count is
        requested).

>How-To-Repeat:

        % yes | uniq

        Will not display anything (until you kill yes). With my version,
        uniq will immediately output the first ``y''.

>Fix:

cvs server: Diffing .
Index: uniq.1
===================================================================
RCS file: /home/ncvs/src/usr.bin/uniq/uniq.1,v
retrieving revision 1.13
diff -U2 -r1.13 uniq.1
--- uniq.1      2002/07/05 09:44:47     1.13
+++ uniq.1      2002/10/04 21:52:55
@@ -130,4 +130,14 @@
 .Fl Ns Ar number
 options have been deprecated but are still supported in this implementation.
+.Pp
+Unlike in many other implementations, the option
+.Fl c
+can be combined with either of the
+.Fl d
+or
+.Fl u
+options and produces the expected output. In case of
+.Fl u
+, for example, each line output is, predictably, prefixed with the number 1.
 .Sh SEE ALSO
 .Xr sort 1
Index: uniq.c
===================================================================
RCS file: /home/ncvs/src/usr.bin/uniq/uniq.c,v
retrieving revision 1.24
diff -U2 -r1.24 uniq.c
--- uniq.c      2002/09/04 23:29:08     1.24
+++ uniq.c      2002/10/04 21:52:56
@@ -116,8 +116,5 @@
 
        /* If no flags are set, default is -d -u. */
-       if (cflag) {
-               if (dflag || uflag)
-                       usage();
-       } else if (!dflag && !uflag)
+       if (!dflag && !uflag)
                dflag = uflag = 1;
 
@@ -159,16 +156,30 @@
 
                if (comp) {
-                       if (cflag || !dflag || !uflag)
+                       /*
+                        * Ok, a different line found. See if we
+                        * should output it now, or wait, or if
+                        * it was output already...
+                        */
+                       if (cflag || (!dflag && uflag && !repeats))
                                show(ofp, prevline);
+                       else if (!cflag && dflag && uflag)
+                               show(ofp, thisline);
                        t1 = prevline;
                        prevline = thisline;
-                       if (!cflag && uflag && dflag)
-                               show(ofp, prevline);
                        thisline = t1;
                        repeats = 0;
-               } else
+               } else {
                        ++repeats;
+                       /*
+                        * This line repeats the previous one. If we
+                        * we want the duplicate lines only, without
+                        * the total count, we can safely output
+                        * it now if this is the first repetition.
+                        */
+                       if (!cflag && dflag && repeats == 1 && !uflag)
+                               show(ofp, thisline);
+               }
        }
-       if (cflag || !dflag || !uflag)
+       if (cflag || (!dflag && uflag && !repeats))
                show(ofp, prevline);
        exit(0);
@@ -200,9 +211,8 @@
 show(FILE *ofp, char *str)
 {
-
-       if (cflag && *str)
-               (void)fprintf(ofp, "%4d %s\n", repeats + 1, str);
-       if ((dflag && repeats) || (uflag && !repeats))
+       if (!cflag)
                (void)fprintf(ofp, "%s\n", str);
+       else if (*str && ((dflag && repeats) || (uflag && !repeats)))
+               (void)fprintf(ofp, "%4d %s\n", repeats + 1, str);
 }

>Release-Note:
>Audit-Trail:
>Unformatted:

To Unsubscribe: send mail to majordomo@FreeBSD.org
with "unsubscribe freebsd-bugs" in the body of the message




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?200210041758.06299.mi%2Bmx>