使用awk格式化数字

时间:2014-02-25 14:53:20

标签: awk number-formatting

我有几个包含许多列和数千行的文件。大多数条目都是浮点数,精度太高,有些则以科学计数法给出。我正在尝试使用awk重新格式化所有数字,但我对awk不是很熟悉。我试过以下

cat bs04_diff_all_e.csv | awk -v OFS="," -v OFMT="%.2f" -F"," 'NR == 45 {printf $1,$2,$3,$4,$11,$12,$13,$14,$15,$16,$17,$18,$19}' 

哪个输出

648376.819998

只。即行中的第一篇文章,未格式化。

cat bs04_diff_all_e.csv | awk -v OFS="," -v OFMT="%.2f" -F"," 'NR == 45 {print $1,$2,$3,$4,$11,$12,$13,$14,$15,$16,$17,$18,$19}' 

输出

648376.819998,7537575.31999,1692.0,34.99202,0.30931,0.0054,0.07764,0.01284,0.00849,0.04081,0.0403,0.00946,-9e-05

所以我得到了列,但格式仍然不起作用。我已经四处寻找解决方案而且看不出为什么这不起作用但是正如我之前所说,我不熟悉awk。我究竟做错了什么?我是否真的需要单独指定所有44列的数字格式?

1 个答案:

答案 0 :(得分:0)

忘记OFMT变量,并在变量前使用格式字符串sprintf()。上一个END {}部分删除了逗号:

cat bs04_diff_all_e.csv | 
    awk -F"," '
        BEGIN { split("1,2,3,4,11", l, /,/) } 
        { for (i in l) { s = s sprintf( "%.2f,", $(l[i]) ) } }
        END { sub(/,$/, "\n", s); print s }
    '

它产生类似于:

648376.82,7537575.32,1692.00,34.99,0.04