linux中awk命令实现科学计数法和普通数值的相互转换
001、
root@PC1:/home/test2# ls test.txt root@PC1:/home/test2# cat test.txt ## 测试数据 chr logl_H1 l_remle p_wald 1 -3.078836e+03 1.000000e+05 6.973924e-01 1 -3.078583e+03 1.000000e+05 4.170767e-01 1 -3.078921e+03 1.000000e+05 9.387895e-01 1 -3.076761e+03 1.000000e+05 3.819984e-02 1 -3.078793e+03 1.000000e+05 6.359079e-01 1 -3.078882e+03 1.000000e+05 8.279424e-01 1 -3.077049e+03 1.000000e+05 5.450867e-02 1 -3.078782e+03 1.000000e+05 6.191698e-01 1 -3.078870e+03 1.000000e+05 7.736763e-01 root@PC1:/home/test2# awk '{for(i = 1; i <= NF; i++) {printf("%.2f\t", $i)}; {printf("\n")}}' test.txt 0.00 0.00 0.00 0.00 ## 转换浮点表示 1.00 -3078.84 100000.00 0.70 1.00 -3078.58 100000.00 0.42 1.00 -3078.92 100000.00 0.94 1.00 -3076.76 100000.00 0.04 1.00 -3078.79 100000.00 0.64 1.00 -3078.88 100000.00 0.83 1.00 -3077.05 100000.00 0.05 1.00 -3078.78 100000.00 0.62 1.00 -3078.87 100000.00 0.77 root@PC1:/home/test2# awk '{for(i = 1; i <= NF; i++) {printf("%.4f\t", $i)}; {printf("\n")}}' test.txt 0.0000 0.0000 0.0000 0.0000 ## 转换为浮点,四位小数 1.0000 -3078.8360 100000.0000 0.6974 1.0000 -3078.5830 100000.0000 0.4171 1.0000 -3078.9210 100000.0000 0.9388 1.0000 -3076.7610 100000.0000 0.0382 1.0000 -3078.7930 100000.0000 0.6359 1.0000 -3078.8820 100000.0000 0.8279 1.0000 -3077.0490 100000.0000 0.0545 1.0000 -3078.7820 100000.0000 0.6192 1.0000 -3078.8700 100000.0000 0.7737
002、数值转换为科学计数法
root@PC1:/home/test2# ls test.txt root@PC1:/home/test2# cat test.txt 0.00000000 0.00000000 0.00000000 0.00000000 1.00000000 -3078.83600000 100000.00000000 0.69739240 1.00000000 -3078.58300000 100000.00000000 0.41707670 1.00000000 -3078.92100000 100000.00000000 0.93878950 1.00000000 -3076.76100000 100000.00000000 0.03819984 1.00000000 -3078.79300000 100000.00000000 0.63590790 1.00000000 -3078.88200000 100000.00000000 0.82794240 1.00000000 -3077.04900000 100000.00000000 0.05450867 1.00000000 -3078.78200000 100000.00000000 0.61916980 1.00000000 -3078.87000000 100000.00000000 0.77367630 root@PC1:/home/test2# awk '{for(i = 1; i <= NF; i++) {printf("%e\t", $i)}; {printf("\n")}}' test.txt 0.000000e+00 0.000000e+00 0.000000e+00 0.000000e+00 1.000000e+00 -3.078836e+03 1.000000e+05 6.973924e-01 1.000000e+00 -3.078583e+03 1.000000e+05 4.170767e-01 1.000000e+00 -3.078921e+03 1.000000e+05 9.387895e-01 1.000000e+00 -3.076761e+03 1.000000e+05 3.819984e-02 1.000000e+00 -3.078793e+03 1.000000e+05 6.359079e-01 1.000000e+00 -3.078882e+03 1.000000e+05 8.279424e-01 1.000000e+00 -3.077049e+03 1.000000e+05 5.450867e-02 1.000000e+00 -3.078782e+03 1.000000e+05 6.191698e-01 1.000000e+00 -3.078870e+03 1.000000e+05 7.736763e-01