awk算术运算
2010-05-09 11:07:13 阿炯

1>. 普通运算
[gan@localhost tmp]$ awk 'BEGIN { print 13+3 }' #加
16
[gan@localhost tmp]$ awk 'BEGIN { print 13-3 }' #减
10
[gan@localhost tmp]$ awk 'BEGIN { print 13*3 }' #乘
39
[gan@localhost tmp]$ awk 'BEGIN { print 12/3 }' #除
4
[gan@localhost tmp]$ awk 'BEGIN { print 13/3 }' #除
4.33333
[gan@localhost tmp]$ awk 'BEGIN { print 13%3 }' #求余
1
[gan@localhost tmp]$ awk 'BEGIN { print 13%5 }' #求余
3
[gan@localhost tmp]$ awk 'BEGIN { print 13^2 }' #2次方
169
[gan@localhost tmp]$ awk 'BEGIN { print 13**2 }'        #2次方
169
[gan@localhost tmp]$ awk 'BEGIN { print -2 }'   #普通负数输出
-2

[gan@localhost tmp]$ awk 'BEGIN { print (2+10)/2+((3^2)/100) }' #综合一点的运算
6.09
[gan@localhost tmp]$ awk 'BEGIN { x="123"; print x-2 }' #将字符串转换为数值来运算
121

2>. awk调用数学函数运算
函数名称            返回值
atan2(x,y)    y,x范围内的余切
cos(x)      余弦函数
exp(x)      求幂
int(x)      取整
log(x)      自然对数
rand()      随机数
sin(x)      正弦
sqrt(x)     平方根
srand(x)    x是rand()函数的种子

[gan@localhost tmp]$ awk 'BEGIN { print atan2(1,2) }'
0.463648
[gan@localhost tmp]$ awk 'BEGIN { print cos(3) }'
-0.989992
[gan@localhost tmp]$ awk 'BEGIN { print cos(0.9) }'
0.62161
[gan@localhost tmp]$ awk 'BEGIN { print int(1.9923) }'
1
[gan@localhost tmp]$ awk 'BEGIN { print int(4.9923) }'
4
[gan@localhost tmp]$ awk 'BEGIN { print exp(3) }'
20.0855
[gan@localhost tmp]$ awk 'BEGIN { print exp(4) }'
54.5982
[gan@localhost tmp]$ awk 'BEGIN { print log(4) }'
1.38629
[gan@localhost tmp]$ awk 'BEGIN { print rand() }'
0.237788
[gan@localhost tmp]$ awk 'BEGIN { print rand() }'
0.237788
[gan@localhost tmp]$ awk 'BEGIN { print rand(32) }'
awk: fatal: 1 is invalid as number of arguments for rand
[gan@localhost tmp]$ awk 'BEGIN { print sin(32) }'
0.551427
[gan@localhost tmp]$ awk 'BEGIN { print sqrt(4) }'
2
[gan@localhost tmp]$ awk 'BEGIN { print srand(4) }'
1
[gan@localhost tmp]$ awk 'BEGIN { print srand(10) }'
1
[gan@localhost tmp]$ awk 'BEGIN { srand(10); print rand()}'
0.255219
[gan@localhost tmp]$ awk 'BEGIN { srand(10); print rand() }'
0.255219
[gan@localhost tmp]$ awk 'BEGIN { srand(1000); print rand() }'
0.524085
[gan@localhost tmp]$ awk 'BEGIN { srand(10); print rand() }'
0.255219
[gan@localhost tmp]$ awk 'BEGIN { srand(10); print rand() }'
0.255219

3>. 利用数组做复杂的运算
将文件中的数据求和
[gan@localhost ftmp]$ cat file.txt
1
2
3
3
44
66
2
3
78
9900
235
[gan@localhost ftmp]$ awk 'BEGIN {sum=0} {sum+=$0} END{print sum}' file.txt
10337

求平均数:
[gan@localhost ftmp]$ awk 'BEGIN {sum=0} {sum+=$0} END{print sum/FNR}' file.txt
939.727

分组求平均:
[gan@localhost ftmp]$ cat gav.txt
1 100 10
1 200 20
1 300 30
1 500 20
2 100 3
2 300 5
[gan@localhost ftmp]$ awk -F" " '{sum2[$1] += $2; sum3[$1] += $3; cnt[$1] += 1} END{for (i in sum2) print i, sum2[i]/cnt[i], sum3[i]/cnt[i]}' gav.txt
1 275 20
2 200 4

数字的awk减法
我有两个文件,其记录值如下,现将此两文件地值做相减操作:
==> file1 <==
6
7
8
9
10

==> file2 <==
5
4
3
2
1
方法一)hto@debian:~/bin$ awk 'getline f1 < "file1" { print $1 - f1 }' file2
-1
-3
-5
-7
-9
方法二)hto@debian:~/bin$ paste file1 file2 | awk '{ print $2 - $1 }'
-1
-3
-5
-7
-9
方法三)hto@debian:~/bin$ paste -d- file2 file1|bc
-1
-3
-5
-7
-9

该文章最后由 阿炯 于 2012-04-26 09:47:40 更新,目前是第 2 版。