排序,唯一并显示最大计数

时间:2013-03-15 19:46:23

标签: unix sorting awk uniq

在下表中,删除重复项,并在某些时间点说12:06只显示最大计数值

3/12/2013 12:00 639 Count
3/12/2013 12:06 693 Count
3/12/2013 12:12 636 Count
3/12/2013 12:18 649 Count
3/12/2013 12:24 658 Count
3/12/2013 12:30 926 Count
3/12/2013 12:36 721 Count
3/12/2013 12:42 797 Count
3/12/2013 12:48 717 Count
3/12/2013 12:00 639 Count
3/12/2013 12:06 700 Count
3/12/2013 12:12 636 Count
3/12/2013 12:18 649 Count
3/12/2013 12:24 658 Count
3/12/2013 12:30 726 Count
3/12/2013 12:36 721 Count
3/12/2013 12:42 850 Count
3/12/2013 12:48 900 Count
3/12/2013 12:00 639 Count
3/12/2013 12:06 693 Count
3/12/2013 12:12 636 Count
3/12/2013 12:18 649 Count
3/12/2013 12:24 658 Count
3/12/2013 12:30 926 Count
3/12/2013 12:36 721 Count
3/12/2013 12:42 797 Count
3/12/2013 12:48 950 Count

输出应如下所示:

3/12/2013 12:00 639 Count
3/12/2013 12:06 700 Count
3/12/2013 12:12 636 Count
3/12/2013 12:18 649 Count
3/12/2013 12:24 658 Count
3/12/2013 12:30 926 Count
3/12/2013 12:36 721 Count
3/12/2013 12:42 850 Count
3/12/2013 12:48 900 Count

1 个答案:

答案 0 :(得分:0)

您需要sortuniq

$ sort -k2,2 -rk3,3  file | uniq -w15 | sort -k2,2
3/12/2013 12:00 639     Count
3/12/2013 12:06 700     Count
3/12/2013 12:12 636     Count
3/12/2013 12:18 649     Count
3/12/2013 12:24 658     Count
3/12/2013 12:30 926     Count
3/12/2013 12:36 721     Count
3/12/2013 12:42 850     Count
3/12/2013 12:48 950     Count

或某些awk

$ awk '{if($3>a[$1" "$2])a[$1" "$2]=$3}END{for(k in a)print k,a[k],"\tCount"}' file | sort
3/12/2013 12:00 639     Count
3/12/2013 12:06 700     Count
3/12/2013 12:12 636     Count
3/12/2013 12:18 649     Count
3/12/2013 12:24 658     Count
3/12/2013 12:30 926     Count
3/12/2013 12:36 721     Count
3/12/2013 12:42 850     Count
3/12/2013 12:48 950     Count