not getting the latest against the uniq with awk

48 views Asked by At
65 20150427 000000000
8 20120930 000000000
18 20130626 000000000
6 20140505 000000000
1 20150603 000000000
18 20140712 000000000
65 20150502 000000000
10 20150113 000000000
92 20140707 000001000
20 20130530 000000000
11 20141231 000000000
15 20140516 000000000
1 20150523 000000000
18 20130620 120014000
7 20140505 000000000

Above is the file. First col is the uniq id followed by date & time. Whenever i am running the below query am not getting the desired result...

awk '{a[$1]=$1}END{for(i in a) print i,a[i]}' file

it is showing the random date but i want the latest date to be displayed against each uniq with no repetition. Please suggest a way forward.

2

There are 2 answers

0
anubhava On

You can use this awk instead:

awk '!a[$1] || $2>a[$1]{a[$1]=$2} END{for (i in a) print i, a[i]}' file
1 20150603
6 20140505
7 20140505
8 20120930
10 20150113
11 20141231
15 20140516
18 20140712
20 20130530
65 20150502
92 20140707
0
karakfa On
$ sort -rn datetime.txt | sort -n -u
1 20150603 000000000
6 20140505 000000000
7 20140505 000000000
8 20120930 000000000
10 20150113 000000000
11 20141231 000000000
15 20140516 000000000
18 20140712 000000000
20 20130530 000000000
65 20150502 000000000
92 20140707 000001000