in reply to Separate duplicate and unique records

Note: I know this is Seekers of Perl Wisdom, but there were already plenty of good Perl solutions, and I couldn't resist.

If you're on a unix (should I say GNU?) machine, or have cygwin installed on a Windows machine, you can very easily accomplish these tasks with sort and uniq.

Print unique lines:

sort -u numberlist

Print all duplicates:

sort numberlist | uniq -D

(Or if you know the numberlist is already sorted, uniq -D numberlist will do.)

Replies are listed 'Best First'.
Re: Re: Separate duplicate and unique records
by Thelonius (Priest) on Aug 07, 2003 at 21:43 UTC
    Almost right. sort -u will give one copy of each input record whether it was unique or not to begin with. What he wants is uniq -u and uniq -D since he stated the input was already sorted. My program below does it in one pass, though.

      Well, sort -u (or simply the default behavior of uniq) matches the behavior of the OP's sample code -- that is, it prints all values once, but only once. Perhaps that is not what he wanted, but if he stated that in his original post, it's not clear to me. Either way, it seems he has many solutions to any number of problems, some of which may be the actual problem he was looking for help with. :)