How To sort and find duplicate records

Hi,
I'm was asked to sort a password file, to show all duplicate users and their UIDs.
For Example if you have a file like this:
UID    UserName   Password
----   --------   ----------
1236   john       oe93kf9034j
936    max        fkl03kf032j
9381   keren      fg38uf6124t
8988   john       fj3589gjf01
9834   roma       fgj390jf203
8915   max        gf23j09g305
341    john       mf9244t24t4
9841   david      f02jflp3053
43745  david      23hkfg03g35
4956   ron        04fk054f2e4
69595  max        kvf9035g022
7765   john       mg30gk30gkw

I want to show all the duplicate users and all their UIDs like this:
john - 1236 8988 341 7765
max - 936 8915 69595
david - 9841 43745

Solution
To find all duplicate users we'll use the next script:
$ cut -f2 yourFile | sort | uniq -d
it'll display:
john
max
david

Now we want to know all the UIDs of those users
create a new file with vi
$ vi dup_users.sh
and copy the folloing lines:
dup=`cut -f2 yourFile | sort | uniq -d`

for a in $dup
do
        echo $a - `cat yourFlie | grep $a | cut -f1`
done

It will search for each user all UIDs he has and display them like the example on the top.

More
$ sort yourFlie | uniq -u # only the unique, non-repeated lines
$ sort yourFile | uniq -d # repeated lines
$ sort yourFile | uniq -c # all lines + count repeated lines


1 comment: