From owner-freebsd-bugs Fri Oct 4 15: 0:24 2002 Delivered-To: freebsd-bugs@hub.freebsd.org Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125]) by hub.freebsd.org (Postfix) with ESMTP id 0406837B401 for ; Fri, 4 Oct 2002 15:00:20 -0700 (PDT) Received: from freefall.freebsd.org (freefall.freebsd.org [216.136.204.21]) by mx1.FreeBSD.org (Postfix) with ESMTP id E70F143E3B for ; Fri, 4 Oct 2002 15:00:18 -0700 (PDT) (envelope-from gnats@FreeBSD.org) Received: from freefall.freebsd.org (gnats@localhost [127.0.0.1]) by freefall.freebsd.org (8.12.6/8.12.6) with ESMTP id g94M0ICo068676 for ; Fri, 4 Oct 2002 15:00:18 -0700 (PDT) (envelope-from gnats@freefall.freebsd.org) Received: (from gnats@localhost) by freefall.freebsd.org (8.12.6/8.12.6/Submit) id g94M0IXE068675; Fri, 4 Oct 2002 15:00:18 -0700 (PDT) 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 To: FreeBSD-gnats-submit@FreeBSD.org Cc: tjr@FreeBSD.org Subject: bin/43675: uniq prints last, not first of the identical lines [patch] Sender: owner-freebsd-bugs@FreeBSD.ORG Precedence: bulk List-ID: List-Archive: (Web Archive) List-Help: (List Instructions) List-Subscribe: List-Unsubscribe: X-Loop: FreeBSD.org >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