Sorting 排序以显示第1列中每个值的前2个条目
我有一个表,它包含3列,如下所示。产品品牌和产品列表(第1列和第2列)的条目数不到100条Sorting 排序以显示第1列中每个值的前2个条目,sorting,unix,awk,Sorting,Unix,Awk,我有一个表,它包含3列,如下所示。产品品牌和产品列表(第1列和第2列)的条目数不到100条 Product Brand | Product | Product Revenue A | biscuit | 20 B | bread | 33 C | candy | 41 A | frozen |
Product Brand | Product | Product Revenue
A | biscuit | 20
B | bread | 33
C | candy | 41
A | frozen | 52
B | icecream | 89
B | veggies | 65
C | juice | 43
D | coffee | 78
E | tea | 21
C | chips | 44
A | chips | 29
使用unix
命令,我是否可以获得每个品牌的前2名销售产品(按收入降序排列)
预期产量
Product Brand | Product | Product Revenue
A | frozen | 52
A | chips | 29
B | icecream | 89
B | veggies | 65
C | chips | 44
C | juice | 43
D | coffee | 78
etc
请您尝试以下内容,并让我知道这是否有助于您
sort -t"|" -k1.1 -k3.1 Input_file | tac | awk -F' +| +' 'NR>1{if(++a[$1]<3){print | "sort -k1"};next} 1'
sort-t“|”-k1.1-k3.1输入_文件| tac | awk-F'+|+''NR>1{if(++a[$1]1{
如果(++a[$1]head-n1测试&tail-n+2测试| sort-k1,1-k3r,3 | awk'word!=$1{count=1;word=$1}countsort-k1,1-k5,5nr文件| awk'a[$1]++
sort -t"|" -k1.1 -k3.1 Input_file |
tac |
awk -F' +| +' '
NR>1{
if(++a[$1]<3){
print | "sort -k1"
};
next
}
1'
head -n1 test && tail -n+2 test | sort -k1,1 -k3r,3 | awk 'word!=$1{count=1;word=$1} count<=2{print; count++}'
Product Brand | Product | Product Revenue
A | frozen | 52
A | chips | 29
B | veggies | 65
B | icecream | 89
C | juice | 43
C | chips | 44
D | coffee | 78
E | tea | 21
sort -k1,1 -k5,5nr file | awk 'a[$1]++<2'