uniq without sort <-------------- GURU NEEDED

Discussion in 'C Programming' started by gnuist006@gmail.com, Jan 25, 2008.

  1. Guest

    This is a tough problem, and needs a guru.

    I know it is very easy to find uniq or non-uniq lines if you scramble
    all of them and sort them. Its trivially

    echo -e "a\nc\nd\nb\nc\nd" | sort | uniq

    $ echo -e "a\nc\nd\nb\nc\nd"
    a
    c
    d
    b
    c
    d

    $ echo -e "a\nc\nd\nb\nc\nd"|sort|uniq
    a
    b
    c
    d


    So it is TRIVIAL with sort.

    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.

    Thanks to the star who can help
    gnuist
     
    , Jan 25, 2008
    #1
    1. Advertising

  2. On Thu, 24 Jan 2008 18:45:24 -0800 (PST), 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
     
    Michele Dondi, Jan 29, 2008
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Martin Foster

    Perl script to mimic uniq

    Martin Foster, Jan 30, 2004, in forum: Perl
    Replies:
    9
    Views:
    709
    Aaron Sherman
    Feb 5, 2004
  2. Navin
    Replies:
    1
    Views:
    699
    Ken Schaefer
    Sep 9, 2003
  3. Markus

    Array#uniq

    Markus, Sep 29, 2004, in forum: Ruby
    Replies:
    1
    Views:
    101
    Yukihiro Matsumoto
    Sep 30, 2004
  4. Belorion

    Array::uniq { block } ?

    Belorion, Jan 26, 2005, in forum: Ruby
    Replies:
    22
    Views:
    282
    David A. Black
    Jan 31, 2005
  5. Bil Kleb
    Replies:
    5
    Views:
    123
Loading...

Share This Page