需要重新排列,并在solaris命令列求和 [英] need to rearrange and sum column in solaris command

查看:122
本文介绍了需要重新排列,并在solaris命令列求和的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面我有一个名为atp.csv文件数据

I have below data named atp.csv file

Date_Time,M_ID,N_ID,Status,Desc,AMount,Type
2015-01-05 00:00:00 076,1941321748,BD9010423590206,200,Transaction Successful,2000,PRETOP
2015-01-05 00:00:00 077,1941323504,BD9010423590207,351,Transaction Successful,5000,PRETOP
2015-01-05 00:00:00 078,1941321743,BD9010423590205,200,Transaction Successful,1500,PRETOP
2015-01-05 00:00:00 391,1941323498,BD9010500000003,200,Transaction Successful,1000,PRETOP

我想用下面的命令明智计数状态。

i want to count status wise using below command.

cat atp.csv|awk -F',' '{print $4}'|sort|uniq -c

输出类似如下:

3 200
1 351

但我要像下面的输出,也想在状态总结量列明智的。

But i want to like below output and also want to sum the amount column in status wise.

200,3,4500
351,1,5000

这是状态,然后再算上value.Please帮助..

That is status is first and then count value.Please help..

推荐答案

AWK有关联数组。

AWK has associative arrays.

% cat atp.csv | awk -F, 'NR>1 {n[$4]+=1;s[$4]+=$6;} END {for (k in n) { print k "," n[k] "," s[k]; }}' | sort
200,3,4500
351,1,5000

在上面的:


  1. 第一行(记录)将被跳过, NR方式> 1

N [K] 是关键 K 中出现的次数(只要加1)和 S [K] 是场6运行和值(让我们添加 $ 6 )。

n[k] is the number of occurrences of key k (so we add 1), and s[k] is the running sum values in field 6 (so we add $6).

最后,所有记录都处理完后( END ),您可以通过相关的阵列通过键(对于N中(K迭代){...} ),并打印数组键和值 N 取值与密钥关联。

Finally, after all records are processed (END), you can iterate over associated arrays by key (for (k in n) { ... }) and print the keys and values in arrays n and s associated with the key.

这篇关于需要重新排列,并在solaris命令列求和的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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