AWK组由多个列和打印最大值与非主键 [英] awk group by multiple columns and print max value with non-primary key

查看:164
本文介绍了AWK组由多个列和打印最大值与非主键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来这个网站,并努力学习AWK。我试图找到FIELD3的最大值,由FIELD1分组和打印所有与最大值的字段。现场2包含的时间,这意味着每个项目1有场2,FIELD3 96值和字段4

i'm new to this site and trying to learn awk. i'm trying to find the maximum value of field3, grouping by field1 and print all the fields with maximum value. Field 2 contains time, that means for each item1 there is 96 values of field2,field3 and field4

输入文件:(逗号分隔)

input file: (comma separated)

item1,00:15,10,30
item2,00:45,20,45
item2,12:15,30,45
item1,00:30,20,56
item3,23:00,40,44
item1,12:45,50,55
item3,11:15,30,45

所需的输出:

item1,12:45,50,55
item2,12:15,30,45
item3,11:15,30,45

我试过到目前为止:

what i tried so far:

BEGIN{
FS=OFS=","}
{
if (a[$1]<$3){
   a[$1]=$3}
}
END{
for (i in a ){
print i,a[i]
}

但这只是打印

item1,50
item2,30
item3,30

但我需要打印相应字段2和字段4的最大值,如图所需的输出。请帮助。

but i need to print the corresponding field2 and field4 with the max value as shown in the desired output. please help.

推荐答案

这里的问题是,你是不是存储全线飘红,所以当你通过最终的数据没有完整的数据进行打印。

The problem here is that you are not storing the whole line, so when you go through the final data there is no full data to print.

您需要做的就是用另一个数组,说数据[指数] =全系列

What you need to do is to use another array, say data[index]=full line:

BEGIN{
FS=OFS=","}
{
 if (a[$1]<$3){
   a[$1]=$3
   data[$1]=$0}       # store it here!
}
END {
   for (i in a )
       print data[i]  # print it here
}

或者作为一个班轮:

Or as a one-liner:

$ awk 'BEGIN{FS=OFS=","} {if (a[$1]<$3) {a[$1]=$3; data[$1]=$0}} END{for (i in a) print data[i]}' file
item1,12:45,50,55
item2,12:15,30,45
item3,23:00,40,44

这篇关于AWK组由多个列和打印最大值与非主键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆