gpt4 book ai didi

java - 如何按列值分组到行和列标题中,然后动态对列求和

转载 作者:太空宇宙 更新时间:2023-11-04 05:21:34 25 4
gpt4 key购买 nike

下面是我的输入和输出.txt文件。

我想按StatusDate对数据进行分组和Method 。然后根据 StatusDate 对值求和和Method .

输入.txt

No,Date,MethodStatus,Key,StatusDate,Hit,CallType,Method,LastMethodType
112,12/15/16,Suceess,Geo,12/15/16,1,Static,GET,12/15/16
113,12/18/16,Suceess,Geo,12/18/16,1,Static,GET,12/18/16
114,12/19/16,AUTHORIZED,Geo,12/19/16,1,Static,GET,12/19/16
115,12/19/16,AUTHORIZED,Geo,12/19/16,1,Static,GET,12/19/16
116,12/19/16,Suceess,Geo,12/19/16,1,Static,PUT,12/19/16
117,12/19/16,Suceess,Geo,12/19/16,1,Static,PUT,12/19/16
118,12/19/16,Waiting,Geo,12/19/16,1,Static,GET,12/19/16
119,12/19/16,AUTHORIZED,Geo,12/19/16,1,Static,GET,12/19/16
120,12/17/16,Suceess,Geo,12/17/16,1,Static,GET,12/17/16
121,12/17/16,Suceess,Geo,12/17/16,1,Static,GET,12/17/16
130,12/16/16,Suceess,Geo,12/16/16,1,Static,GET,12/16/16

Out.txt

StatusDate,12/15/16,12/16/16,12/17/16,12/17/16,12/18/16,12/19/16,12/19/16,12/19/16,12/19/16,12/19/16,12/19/16,Grand Total
GET,1,1,1,1,1,1,1,1,1,,,9
PUT,,,,,,,,,,1,1,2
Grand Total,1,1,1,1,1,1,1,1,1,1,1,11

我正在使用awk并将数据分割为 awk -F, '{if($8=="GET") print }' ,然后计算总和值。由于文件较大,因此存在延迟。

是否可以一步完成所有事情?那么文件操作会减少吗?

最佳答案

您可以使用如下所示的 GNU awk 脚本:

脚本.awk

BEGIN { PROCINFO["sorted_in"] = "@ind_str_asc" }

function remember( theDate, mem) {
mem[ theDate] +=1
# in Totals the column sum is stored for each possible date (i.e the columns)
Totals[theDate] += 1
}

# with header 0 or 1 the first line in output is differentiated
# OFS is used, so it is possible to use a commandline option like
# -v OFS='\t' or -v OFS=','
function printMem( mem, name, header ) {
printf("%s%s",name,OFS)
sum=0
for( k in Totals ) {
if( header)
printf("%s%s", k, OFS )
else {
printf("%s%s", mem[k], OFS )
sum += mem[k]
}
}
if(!header)
printf("%s", sum )
else
printf("Grand Total")
print ""
}

# different methods are stored in different arrays
$8 == "GET" { remember( $2, get ) }
$8 == "PUT" { remember( $2, put ) }

END { # print the stored values
# the first line header
printMem( Totals , "StatusDate", 1)
printMem( get , "GET", 0)
printMem( put , "PUT", 0)
# the summary line
printMem( Totals , "Grand Total", 0)
}

像这样运行脚本:awk -F, -v OFS=',' script.awk Input.txt

关于java - 如何按列值分组到行和列标题中,然后动态对列求和,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41331731/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com