awk 算术运算

测试环境:
[gan@localhost tmp]$ uname -a
Linux localhost.localdomain 2.6.25.4-10.fc8 #1 SMP Thu May 22 23:34:09 EDT 2008 i686 i686 i386 GNU/Linux
[gan@localhost tmp]$ awk -W version
GNU Awk 3.1.5
......
http://gan.cublog.cn
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
http://gan.cublog.cn
[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
http://gan.cublog.cn
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

发表评论

电子邮件地址不会被公开。 必填项已用*标注

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据