开发者

awk histogram in buckets

开发者 https://www.devze.com 2023-03-26 13:10 出处:网络
Consider I have a following file.. 1 a 1 b 1 a 1 c 1 a 2 a 2 d 2 a 2 d I want to have a histogram within a bucket... for example if bucket is 1 then the output will be

Consider I have a following file..

1 a
1 b
1 a
1 c
1 a
2 a
2 d
2 a
2 d

I want to have a histogram within a bucket... for example if bucket is 1 then the output will be

a 3
b 1
c 1
a 2
d 2

for bucket 2... we have

a 5
b 1
c 1
d 2

I want to do it with awk and I literally stuck... here is my code:

awk '
    {A[$1]} count [$2]++ 
    {for(i in A) {print i,A[i]}
    }'开发者_运维技巧 test

Any help?

Thanks,

Amir.


Edit Adding a size_of_bucket variable.

awk -v "size_of_bucket=2" '
  {
    bucket = int(($1-1)/size_of_bucket);
    A[bucket","$2]++;
  }
  END {
    for (i in A) {
      print i, A[i];
    }
  }
'
0

精彩评论

暂无评论...
验证码 换一张
取 消