awk 算术运算

转自:http://hi.baidu.com/qqlaputa/item/91fd5e9584120535336eeb90


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

  • 2
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值