Re: uniq without sort <-------------- GURU NEEDED

Re: uniq without sort <-------------- GURU NEEDED

am 29.01.2008 14:16:28 von Michele Dondi

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

Re: uniq without sort <-------------- GURU NEEDED

am 29.01.2008 17:03:42 von Michael Tosch

Michele Dondi wrote:
> 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!)
>
>

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

Re: uniq without sort <-------------- GURU NEEDED

am 29.01.2008 23:47:38 von Michele Dondi

On Tue, 29 Jan 2008 17:03:42 +0100, Michael Tosch
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{pop^pop}->(map substr
(($a||=join'',map--$|x$_,(unpack'w',unpack'u','G^ ..'KYU;*EVH[.FHF2W+#"\Z*5TI/ER 256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,

Re: uniq without sort <-------------- GURU NEEDED

am 30.01.2008 00:38:37 von jurgenex

Michael Tosch wrote:
>Michele Dondi wrote:
>> On Thu, 24 Jan 2008 18:45:24 -0800 (PST), gnuist006@gmail.com wrote:
>>> I want uniq without sorting the initial order.

>> In shell I don't know. In Perl it's well known to be as trivial as
>>
>> perl -ne 'print unless $saw{$_}++' 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}'

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

jue