remove duplicated lines without sort

Hi
Just wondering whether or not I can remove duplicated lines without sort

For example, I use the command who, which shows users who are logging on. In some cases, it shows duplicated lines of users who are logging on more than one terminal.

Normally, I would do

who | cut -d" " -f1 | sort |uniq

But I realised that I do not want data to be sorted.

Is there any other ways to do it?

Are the duplicated lines adjacent? If not which one do you remove?

Try follow one, use awk array to remember the appearance of the line:

awk '{
 if (arr[$0]=="")
 arr[$0]=1
 }
 END{
 for (i in arr)
 print i
 }' filename

I am new to awk or you can say UNIX. Honestly, I do not understand awk at the moment.
I will try to study it. :slight_smile:
Thank you for your reply anyway.

Duplicated lines are not adjacent and I can remove any of them.

Cheers,

$ who
oracle     pts/1        Aug 21 04:57    (...)
oracle     pts/4        Aug 21 04:57    (...)
$ who|awk '!x[$1]++'
oracle     pts/1        Aug 21 04:57    (...)

If you want only the first field to be displayed:

who|awk '!x[$1]++{print $1}'

Use nawk on Solaris.

perl -ne '$H{$_}++ or print'

remove duplicate without sorting

 who | perl -ane '$H{$F[1]}++ or print'