awk — Arithmetic
Basic arithmetic — add, subtract, multiply, divide, modulo
awk 'BEGIN{print 7+3, 7-3, 7*3, 7/3, 7%3}'
# Output: 10 4 21 2.33333 1
Power and square root
awk 'BEGIN{print 2^10, sqrt(144)}'
# Output: 1024 12
Integer division and remainder
awk 'BEGIN{print int(7/3), 7%3}'
# Output: 2 1
Convert bytes to human-readable (KB, MB, GB)
echo "1073741824" | awk '{
if($1>=1073741824) printf "%.2f GB\n",$1/1073741824
else if($1>=1048576) printf "%.2f MB\n",$1/1048576
else if($1>=1024) printf "%.2f KB\n",$1/1024
else printf "%d B\n",$1
}'
Decimal to hex, octal, binary
awk 'BEGIN{printf "hex=%x oct=%o\n", 255, 255}'
# Output: hex=ff oct=377
Fahrenheit to Celsius
echo "98.6" | awk '{printf "%.1f°F = %.1f°C\n", $1, ($1-32)*5/9}'
Percentage calculation
echo "750 1000" | awk '{printf "%.1f%%\n", ($1/$2)*100}'
# Output: 75.0%
Epoch to human-readable date (GNU awk)
awk 'BEGIN{print strftime("%Y-%m-%d %H:%M:%S", systime())}'
Calculate time differences in seconds
awk 'BEGIN{
start=mktime("2026 04 11 09 00 00")
end=mktime("2026 04 11 17 30 00")
diff=end-start
printf "%d hours %d minutes\n", int(diff/3600), int((diff%3600)/60)
}'
Logarithms — log (natural), log base 10
awk 'BEGIN{printf "ln(100)=%.4f log10(100)=%.4f\n", log(100), log(100)/log(10)}'
Trigonometry — sin, cos, atan2
awk 'BEGIN{pi=atan2(0,-1); printf "pi=%.6f sin(pi/2)=%.4f cos(0)=%.4f\n", pi, sin(pi/2), cos(0)}'