Skip to content

Instantly share code, notes, and snippets.

@yyuu
Created April 20, 2011 11:18
Show Gist options
  • Save yyuu/931016 to your computer and use it in GitHub Desktop.
Save yyuu/931016 to your computer and use it in GitHub Desktop.
unit conversion in awk
function atoix(a) {
if(a~/Pi?B/) return int(a)*1024^5;
if(a~/Ti?B/) return int(a)*1024^4;
if(a~/Gi?B/) return int(a)*1024^3;
if(a~/Mi?B/) return int(a)*1024^2;
if(a~/Ki?B/) return int(a)*1024^1;
return int(a);
}
function itoax(i) {
if(i>1024^5&&i<=1024^6) return sprintf("%.2fPB", i/1024^5);
if(i>1024^4&&i<=1024^5) return sprintf("%.2fTB", i/1024^4);
if(i>1024^3&&i<=1024^4) return sprintf("%.2fGB", i/1024^3);
if(i>1024^2&&i<=1024^3) return sprintf("%.2fMB", i/1024^2);
if(i>1024^1&&i<=1024^2) return sprintf("%.2fKB", i/1024^1);
return sprintf("%dB", i);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment