awktutorial/08-math-functions.awk

31 lines
747 B
Awk
Executable File

#!/usr/bin/awk -f
# https://www.grymoire.com/Unix/Awk.html
# awk supports a number of mathematical and numerical functions.
# these won't be demonstrated in detail here because there are too
# many and their usage is fairly self-explanatory.
# there are basic mathematical operators:
# +, -, *, /
# math:
# exp, int, log, rand, srand, sqrt
# and
# trig functions:
# cos, sin, atan2,
# the example here will merely show how to validate that an input
# field is a number. use the regex: value ~ /^[0-9]*$/
BEGIN {
print "Enter a number:"
getline var < "-"
if (var ~ /^[0-9]*$/) {
print "The log of " var " is " log(var)
} else {
print var " isn't a number. What, are you trying to hack this system?"
}
}