need to rearrange and sum column in solaris command

天涯浪子 提交于 2019-12-01 08:09:34

问题


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

The output is like below:

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

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


回答1:


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

In the above:

  1. The first line (record) is skipped with NR>1.

  2. 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).

  3. 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.




回答2:


You can try this awk version also

awk -F',' '{print $4,",", a[$4]+=$6}' FileName  | sort -r  | uniq -cw 6 | sort -r

Output :

  3 200 , 4500
  1 351 , 5000

Another Way:

awk -F',' '{print $4,",", a[$4]+=$6}' FileName  | sort -r | uniq -cw 6 |sort -r |  sed 's/\([^ ]\+\).\([^ ]\+\).../\2,\1,/'



回答3:


All in (g)awk

awk -F, 'NR>1{a[$4]++;b[$4]+=$6}
         END{n=asorti(a,c);for(i=1;i<=n;i++)print c[i]","a[c[i]]","b[c[i]]}' file


来源:https://stackoverflow.com/questions/27835079/need-to-rearrange-and-sum-column-in-solaris-command

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!