awk — Unit & Base Conversions
Bytes to human-readable with appropriate unit
du -sb /var/log/* 2>/dev/null | awk '{
size=$1
if(size>=1073741824) printf "%8.2f GB %s\n",size/1073741824,$2
else if(size>=1048576) printf "%8.2f MB %s\n",size/1048576,$2
else if(size>=1024) printf "%8.2f KB %s\n",size/1024,$2
else printf "%8d B %s\n",size,$2
}' | sort -rn
Seconds to days/hours/minutes/seconds
echo "86543" | awk '{d=int($1/86400); h=int(($1%86400)/3600); m=int(($1%3600)/60); s=$1%60; printf "%dd %dh %dm %ds\n",d,h,m,s}'
Milliseconds to human-readable latency
echo "1523" | awk '{
if($1>=1000) printf "%.2f seconds\n",$1/1000
else printf "%d ms\n",$1
}'
Temperature conversion — Celsius to Fahrenheit
echo "37" | awk '{printf "%.1f°C = %.1f°F\n", $1, $1*9/5+32}'
Bits per second to human-readable bandwidth
echo "1000000000" | awk '{
if($1>=1e9) printf "%.2f Gbps\n",$1/1e9
else if($1>=1e6) printf "%.2f Mbps\n",$1/1e6
else if($1>=1e3) printf "%.2f Kbps\n",$1/1e3
else printf "%d bps\n",$1
}'
Generate multiplication table
awk 'BEGIN{for(i=1;i<=12;i++){for(j=1;j<=12;j++) printf "%4d",i*j; print ""}}'
Random number generation (seeded)
awk 'BEGIN{srand(); for(i=1;i<=10;i++) printf "%d\n", int(rand()*100)+1}'
Comma-separated thousands formatting
echo "1234567890" | awk '{
s=$1; r=""
while(length(s)>3){r=","substr(s,length(s)-2)r; s=substr(s,1,length(s)-3)}
print s r
}'
# Output: 1,234,567,890