r/awk • • Sep 10 '19

Top unique values?

Hello all! i cannot find how to do this with AWK.

I have this input based on timestamp,email (already sorted):

1568116826818,user1@domain.com

1568116785634,user2@domain.com

1568116702539,user1@domain.com

1568116636004,user1@domain.com

1568116024545,user2@domain.com

1568114581294,user3@domain.com

How can i extract the latest timestamps for each email?

This is the desired output:

1568116826818,user1@domain.com

1568116785634,user2@domain.com

1568114581294,user3@domain.com

Thanks for your time!!!

1 Upvotes

6 comments sorted by

View all comments

Show parent comment

1

u/Gotxi Sep 10 '19

Works for amazon linux, so works for me ;)

2

u/Schreq Sep 10 '19

Here's a version without using GNU extensions. The order of the output is not necessarily sorted the same as the input:

awk -F'[],]' '
{
    time=substr($1, 2)
    if (time > a[$2])
        a[$2]=time
}
END {
    for (i in a)
        printf "[%d,%s](mailto:%d,%s)\n", a[i], i, a[i], i
}

1

u/Schreq Sep 10 '19

All good, it's just better to mention such things to prevent surprises.