与awk类似的Sql组

时间:2016-04-02 05:11:22

标签: unix awk

如何在50%以下,80%以下,80-90%,90%以上显示以下结果组

Filesystem      Size  Used Avail Use% Mounted on
/dev/loop0       18G   15G  2.5G  86% /
none            4.0K     0  4.0K   0% /sys/fs/cgroup
udev            483M  4.0K  483M   1% /dev
tmpfs            99M  1.4M   97M   2% /run
none            5.0M     0  5.0M   0% /run/lock
none            492M  1.8M  490M   1% /run/shm
none            100M   20K  100M   1% /run/user
/dev/sda3       167G  157G  9.9G  95% /host

到现在为止我能够获取挂载点和放大器空间使用awk

df -hk | awk '{print $5 " " $6}'

预期:

0-50%: 7
80-90%: 1
90-100%: 1

1 个答案:

答案 0 :(得分:2)

awk救援!

$ awk -v groups='50 80 90 100' '
          BEGIN{n=split(groups,g);g[0]=0}
               {sub("%","",$5);
                for(i=1;i<=n;i++) 
                   if($5+0<=g[i]) {a[g[i-1]"-"g[i]]++; next}
               } 
            END{for(k in a) print "between: "k"%:",a[k]}' file | 
  sort -k2n

between: 0-50%: 7
between: 80-90%: 1
between: 90-100%: 1