For Programmers: Free Programming Magazines  


Home > Archive > PERL Miscellaneous > January 2008 > Re: uniq without sort <-------------- GURU NEEDED









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Re: uniq without sort <-------------- GURU NEEDED
Michele Dondi

2008-01-29, 8:22 am

On Thu, 24 Jan 2008 18:45:24 -0800 (PST), gnuist006@gmail.com wrote:

>I want uniq without sorting the initial order.
>
>The algorithm is this. For every line, look above if there is another
>line like it. If so, then ignore it. If not, then output it. I am
>sure, I can spend some time to write this in C. But what is the
>solution using shell ? This way I can get an output that preserves the
>order of first occurrence. It is needed in many problems.


In shell I don't know. In Perl it's well known to be as trivial as

perl -ne 'print unless $saw{$_}++' file

(And it's not even the most golfed down solution!)


Michele
--
Se, nella notte in cui concepi' il duce,
Donna Rosa, toccata da divina luce,
avesse dato al fabbro predappiano
invece della fica il deretano,
l'avrebbe presa in culo quella sera
Rosa sola e non l'Italia intera.
- Poesia antifascista
Michael Tosch

2008-01-29, 7:21 pm

Michele Dondi wrote:
> On Thu, 24 Jan 2008 18:45:24 -0800 (PST), gnuist006@gmail.com wrote:
>
>
> In shell I don't know. In Perl it's well known to be as trivial as
>
> perl -ne 'print unless $saw{$_}++' file
>
> (And it's not even the most golfed down solution!)
>
>


It can be transformed to standard awk like this:

awk 'saw[$0]++==0' file

This is "print first unique".
For "print last unique" you really need perl:

perl -ne '$s{$_}=++$i; END {print sort {$s{$a}<=>$s{$b}} keys %s}'

--
Michael Tosch @ hp : com
Michele Dondi

2008-01-29, 7:21 pm

On Tue, 29 Jan 2008 17:03:42 +0100, Michael Tosch
<eedmit@NO.eed.SPAM.ericsson.PLS.se> wrote:

>This is "print first unique".
>For "print last unique" you really need perl:
>
>perl -ne '$s{$_}=++$i; END {print sort {$s{$a}<=>$s{$b}} keys %s}'


Incidentally, you may want to know about $., see perldoc perlvar.


Michele
--
{$_=pack'B8'x25,unpack'A8'x32,$a^=sub{po
p^pop}->(map substr
(($a||=join'',map--$|x$_,(unpack'w',unpack'u','G^<R<Y]*YB='
..'KYU;*EVH[.FHF2W+#"\Z*5TI/ER<Z`S(G.DZZ9OX0Z')=~/./g)x2,$_,
256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,
Jürgen Exner

2008-01-29, 7:21 pm

Michael Tosch <eedmit@NO.eed.SPAM.ericsson.PLS.se> wrote:
>Michele Dondi wrote:
[color=darkred]
[...][color=darkred]
>This is "print first unique".
>For "print last unique" you really need perl:
>
>perl -ne '$s{$_}=++$i; END {print sort {$s{$a}<=>$s{$b}} keys %s}'


Or just read slurp the whole file into an array, reverse(), and then use
Michele's suggestion.

jue
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com