开发者

awk histogram in buckets

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜