数据采用这种格式;-
South,London,5
South,Greenwich,7
South,Guildford,2
South,Maidstone,3
South,Oxted,4
North,Manchester,10
North,Leeds,7
North,Grimsby,9
North,Halifax,11
Midlands,Birmingham,16
Midlands,Coventry,10
Midlands,Walsall,15
Midlands,Rutland,1
我需要一个 AWK 程序来筛选并吐出记录以按第二列进行分组,但只打印前三个值;-
South,Greenwich,7
South,London,5
South,Oxted,4
North,Halifax,11
North,Manchester,10
North,Grimsby,9
Midlands,Birmingham,16
Midlands,Walsall,15
Midlands,Coventry,10
实际文件有更多的地区/城镇 - 有帮助吗?
答案1
您可以首先使用sort
命令对其进行排序,然后使用awk
以下命令过滤每个类别的前 3 个$1
:
sort -t, -k1,1 -k3,3nr file | awk -F, 'n[$1]++<3'
输出:
Midlands,Birmingham,16
Midlands,Walsall,15
Midlands,Coventry,10
North,Halifax,11
North,Manchester,10
North,Grimsby,9
South,Greenwich,7
South,London,5
South,Oxted,4