Subject: bug#10287: [wishlist] uniq can remove non adjacent lines

From: Davide Brini <[email-masked]>
Date: Tue, 13 Dec 2011 09:29:51 +0100

On Mon, 12 Dec 2011 21:20:18 -0700, Bob Proulx <[email-masked]> wrote:

> Stéphane Blondon wrote:
> > I think `uniq` should have an additional option (for example -a,
> > --all) to remove same lines but not adjacent.
> > 
> > The man page explains a workaround based on `sort` but it can be
> > complex to use. Few weeks ago, I had to `uniq`-ize random numbers and
> > the sort couldn't really work. Fortunately, the order was not
> > important so using `sort | uniq | sort --random-sort` was an
> > acceptable solution. I imagine cases based on other tools like `top`
> > could be a problem too.
> 
> If you want to print only the first of a unique line then this perl
> one-liner will do it.
> 
>   perl -lne 'print $_ if ! defined $a{$_}; $a{$_}=$_;'

While we're at it, this is the typical awk way to do that:

awk '!a[$0]++'

-- 
D.