Sunday, March 22, 2015

Recursive Awk - Print size in human readable form

num2h.awk
function human(x) {
  x[1]/=1024; 
  if (x[1]>=1000) { 
      x[2]++; human(x); 
  }
}

 # main   
{  a[1]=$1; a[2]=1; 
   human(a); 
   print a[1],substr("kMGTEPZY",a[2],1) 
}

## alias num2h='awk -f /path/to/num2h.awk <<<'

## >$ num2h 500000
## 488.281 k
## >$ num2h 500000000
## 476.837 M
## >$ num2h 5000000000
## 4.65661 G

No comments:

Post a Comment