gpt4 book ai didi

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

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

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

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

输入.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 }' 分割数据,然后计算总和值。由于文件较大,因此存在延迟。

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

最佳答案

awk -F ',' '
/GET/{ Get[ $2]++}
/PUT}{ Put[ $2]++}
{Total[$2]++}

END {
printf( "StatusDate')
for (d in Total) printf( ",%s", d)
printf( "\nTotal GET")
for (d in Total) printf( ",%d", Get[d])
printf( " PUT")
for (d in Total) printf( ",%d", Put[d])
printf( " Grand Total")
for (d in Total) printf( ",%d", Total[d])
printf( "\n")
}
' Input.txt
  • 将数据存储在每行 3 个数组中
  • 多次循环以在最后显示结果

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

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