scpt6_1.awk
1 2 3 4 5 6 7 8 9 | #!/usr/local/bin/gawk -f
# scpt6_1.awk
BEGIN{
print 7 + 2
print 7 - 2
print 7 * 2
print 7 / 2
}
|
scpt6_1.awk の実行結果は:
[cactus:~/code_awk/tuts]% ./scpt6_1.awk
9
5
14
3.5
scpt6_2.awk
#!/usr/local/bin/gawk -f
# scpt6_2.awk
BEGIN{
x = 5
x += 3 # x = x + 3 と同じ
print x
y = 5
y -= 3 # y = y - 3 と同じ
print y
z = 6
z *= 2 # z = z * 2 と同じ
print z
w = 12
w /= 3 # w = w / 3 と同じ
print w
x = 6
x++ # x += 1 または x = x + 1 と同じ
print x
y = 6
y-- # y -= 1 または y = y - 1 と同じ
print y
}
scpt6_2.awk の実行結果は:
[cactus:~/code_awk/tuts]% ./scpt6_2.awk
8
2
12
4
7
5
awk は数値の計算のために, いくつか独自の組み込み算術関数を用意してくれている.
scpt6_3.awk
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | #!/usr/local/bin/gawk -f
# scpt6_3.awk
# Numeric Functions
# AWK has the following built-in arithmetic functions:
##################################################################################
# atan2(y, x) Returns the arctangent of y/x in radians.
# cos(expr) Returns the cosine of expr, which is in radians.
# exp(expr) The exponential function.
# int(expr) Truncates to integer.
# log(expr) The natural logarithm function.
# rand() Returns a random number N, between 0 and 1, such that 0 <= N < 1.
# sin(expr) Returns the sine of expr, which is in radians.
# sqrt(expr) The square root function.
# srand([expr]) Uses expr as a new seed for the random number generator.
# If no expr is provided, the time of day is used.
# The return value is the previous seed for the random number generator.
##################################################################################
BEGIN{
print atan2(10, 2)
print cos(10)
print exp(10)
print int(23.44)
print int(-23.44)
print rand()
print sin(10)
print sqrt(100)
print sqrt(23)
print srand()
}
|
scpt6_3.awk の実行結果は:
[cactus:~/code_awk/tuts]% ./scpt6_3.awk
1.3734
-0.839072
22026.5
23
-23
0.237788
-0.544021
10
4.79583
1