GNU bug report logs - #44704
uniq: replace repeated lines with a message about how many repeated lines

Previous Next

Package: coreutils;

Reported by: "Brian J. Murrell" <brian <at> interlinx.bc.ca>

Date: Tue, 17 Nov 2020 14:14:01 UTC

Severity: wishlist

Tags: notabug

Full log


Message #27 received at 44704 <at> debbugs.gnu.org (full text, mbox):

From: Bernhard Voelker <mail <at> bernhard-voelker.de>
To: Chris Elvidge <celvidge001 <at> gmail.com>, 44704 <at> debbugs.gnu.org
Cc: "Brian J. Murrell" <brian <at> interlinx.bc.ca>
Subject: Re: bug#44704: uniq: replace repeated lines with a message about how
 many repeated lines
Date: Thu, 19 Nov 2020 01:22:42 +0100
On 11/18/20 12:25 PM, Chris Elvidge wrote:
> You could write your own function to do it. E.g.
> 
> unique() {
> [ "$1" ] || { echo "Needs a readable file to test" && return 1; }
> [ -r "$1" ] || { echo "Needs a readable file to test" && return 1; }
> R=""; N=0
> while IFS=$'\n' read L; do
> [ "$L" = "$R" ] && { ((N++)); continue; }
> [ "$N" -gt 0 ] && { echo "[Previous line repeated $N times]"; N=0; }
> R="$L"
> echo "$L"
> done <$1
> }

Nice.

The UNIX toolbox is diverse. ;-)
I'd use:

awk '
  function p(n) {
    if (n > 1) {
      printf("[previous line repeated %d times]\n", n-1);
    }
  }
  {
    if (line != $0) {
      p(n);
      n = 0;
    }
    line = $0;
    if (n == 0)
      print
    n++;
  }
  END { p(n); }
'

Have a nice day,
Berny




This bug report was last modified 4 years and 272 days ago.

Previous Next


GNU bug tracking system
Copyright (C) 1999 Darren O. Benham, 1997,2003 nCipher Corporation Ltd, 1994-97 Ian Jackson.