From mi+mx@aldan.algebra.com  Fri Oct  4 14:56:29 2002
Return-Path: <mi+mx@aldan.algebra.com>
Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125])
	by hub.freebsd.org (Postfix) with ESMTP
	id EF92337B401; Fri,  4 Oct 2002 14:56:28 -0700 (PDT)
Received: from corbulon.video-collage.com (corbulon.video-collage.com [64.35.99.179])
	by mx1.FreeBSD.org (Postfix) with ESMTP
	id 6214343E65; Fri,  4 Oct 2002 14:56:23 -0700 (PDT)
	(envelope-from mi+mx@aldan.algebra.com)
Received: from misha.murex.com (250-217.customer.cloud9.net [168.100.250.217])
	by corbulon.video-collage.com (8.12.2/8.12.2) with ESMTP id g94LuJ1P092875
	(version=TLSv1/SSLv3 cipher=RC4-MD5 bits=128 verify=FAIL);
	Fri, 4 Oct 2002 17:56:21 -0400 (EDT)
	(envelope-from mi+mx@aldan.algebra.com)
Message-Id: <200210041758.06299.mi+mx@aldan.algebra.com>
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: uniq prints last, not first of the identical lines [patch]

>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:          closed
>Quarter:        
>Keywords:       
>Date-Required:  
>Class:          change-request
>Submitter-Id:   current-users
>Arrival-Date:   Fri Oct 04 15:00:18 PDT 2002
>Closed-Date:    Sun Dec 19 04:21:49 GMT 2004
>Last-Modified:  Sun Dec 19 04:21:49 GMT 2004
>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:
Responsible-Changed-From-To: freebsd-bugs->tjr 
Responsible-Changed-By: mi 
Responsible-Changed-When: Sat Oct 5 06:56:39 PDT 2002 
Responsible-Changed-Why:  
Tim volunteered to take care of this. He saw this patch somewhere 
before :-) 

http://www.freebsd.org/cgi/query-pr.cgi?pr=43675 

From: Giorgos Keramidas <keramida@freebsd.org>
To: bug-followup@freebsd.org
Cc: tjr@freebsd.org, mi+mx@aldan.algebra.com
Subject: Re: bin/43675: uniq prints last, not first of the identical lines [patch]
Date: Tue, 15 Oct 2002 02:19:27 +0300

 Adding to audit trail:
 
 : Message-Id: <20021005150215.A14992@dilbert.robbins.dropbear.id.au>
 : Date: Sat, 5 Oct 2002 15:02:15 +1000
 : From: Tim Robbins <tjr@FreeBSD.ORG>
 : References: <200210041758.06299.mi+mx@aldan.algebra.com>
 :
 : On Fri, Oct 04, 2002 at 05:58:06PM -0400, Mikhail Teterin wrote:
 : > 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).
 :
 : Sorry, I had forgotten about this. I was hesistant about doing it for
 : some reason, but I don't remember why. I'll have another look and see
 : if I can remember what it was.
 :
 : Tim
Responsible-Changed-From-To: tjr->freebsd-bugs 
Responsible-Changed-By: tjr 
Responsible-Changed-When: Sat Feb 14 23:45:10 PST 2004 
Responsible-Changed-Why:  
Unassign due to lack of time and interest. Perhaps someone else 
will pick this up. 

http://www.freebsd.org/cgi/query-pr.cgi?pr=43675 

From: Mikhail Teterin <mi+mx@aldan.algebra.com>
To: freebsd-gnats-submit@freebsd.org, tjr@freebsd.org
Cc:  
Subject: Re: bin/43675: uniq prints last, not first of the identical lines [patch]
Date: Mon, 13 Dec 2004 13:48:18 -0500

 It appears, the problem described in this PR is solved already in the 5.x and, 
 presumably, in the 6.0 -- `yes | uniq' outputs the "y" immediately. Tim, 
 could you, please, propagate the fix into 4.x? Thanks!
 
 	-mi
State-Changed-From-To: open->closed 
State-Changed-By: tjr 
State-Changed-When: Sun Dec 19 04:21:21 GMT 2004 
State-Changed-Why:  
Changed in 5-STABLE and 6-CURRENT; will not be changed in RELENG_4. 

http://www.freebsd.org/cgi/query-pr.cgi?pr=43675 
>Unformatted:
