On Oct 5, 2005, at 3:25 PM, Ryan Leavengood wrote:

> On 10/5/05, Damien Wyart <damien.wyart / free.fr> wrote:
>
>> Hello,
>>
>> Converting from Perl to Ruby, I am trying to find an equivalent to  
>> this
>> Perl one-liner removing duplicate lines in a file (without sorting  
>> it at
>> first) :
>>
>>    perl -ne'$s{$_}++||print' infile >outfile
>>
>> I guess uniq method could be used, but I can't find how.
>>
>
> I tried creating a version that mimics the Perl one (because Ruby also
> has the -n option), but in the end this seemed easier (and much more
> readable):
>
> ruby -e "puts IO.readlines(ARGV[0]).uniq" infile > outfile
>
> So you are right about using uniq.

That slurps the file though, of course, so mind your memory  
requirements.

Here's a more direct translation (untested):

ruby -ne 'BEGIN { $lines = Hash.new(0) }; print if ($lines[$_] += 1)  
== 1' infile > outfile

James Edward Gray II