linux 中awk命令的几个常用算数函数

发布时间 2023-07-13 21:44:53作者: 小鲨鱼2018

 

001、幂函数(即e的多少次方)

[root@PC1 test02]# ls
a.txt
[root@PC1 test02]# cat a.txt         ## 测试数据
9
1
2
10
[root@PC1 test02]# awk '{print exp($1)}' a.txt     ## 幂函数
8103.08
2.71828
7.38906
22026.5

 

02、自然对数(e的多少次方等于x)

[root@PC1 test02]# ls
a.txt
[root@PC1 test02]# cat a.txt    ## 测试数据
9
1
2
2.71828
10
[root@PC1 test02]# awk '{print log($1)}' a.txt   ## 自然对数
2.19722
0
0.693147
0.999999
2.30259

 

03、求平方根

[root@PC1 test02]# ls
a.txt
[root@PC1 test02]# cat a.txt
9
1
2
2.71828
10
[root@PC1 test02]# awk '{print sqrt($1)}' a.txt     ## 求平方跟
3
1
1.41421
1.64872
3.16228

 

04、截断数值至整数部分

[root@PC1 test02]# ls
a.txt
[root@PC1 test02]# cat a.txt
9.8
1.2
2.98
2.71828
10.03
[root@PC1 test02]# awk '{print int($1)}' a.txt       ## 截断数值
9
1
2
2
10

 

05、rand, 取随机数

[root@PC1 test02]# ls
a.txt
[root@PC1 test02]# cat a.txt
9.8
1.2
2.98
2.71828
10.03
[root@PC1 test02]# awk '{print rand()}' a.txt   ## 取随机数, 范围为0~1
0.237788
0.291066
0.845814
0.152208
0.585537

 .