如何转换 kbyte -> gbyte 同一列上的所有值

How can I convert kbyte -> gbyte all values on same column

我有这样的日志文件:

...
h,7,0,0,0
h,8,0,0,0
h,9,1420617572,113390,9777
h,10,1420621172,546702,26577
h,11,1420621754,2746,2786
h,12,0,0,0
...

最后 2 列千字节值。我需要将所有最后 2 列值 kB 更新为 gB。

关于使用列号进行转换和更新的任何建议(awk、sed 等)?

尝试这样的事情:

awk -F, -v OFS="," '{$NF=$NF/1024;$(NF-1)=$(NF-1)/1024}7' file

输出:

h,7,0,0,0
h,8,0,0,0
h,9,1420617572,110.732,9.54785
h,10,1420621172,533.889,25.9541
h,11,1420621754,2.68164,2.7207
h,12,0,0,0

如果你想稍微格式化一下数字:

kent$  awk -F, -v OFS="," '{$NF=sprintf("%.2f",$NF/1024);$(NF-1)=sprintf("%.2f",$(NF-1)/1024)}7' file
h,7,0,0.00,0.00
h,8,0,0.00,0.00
h,9,1420617572,110.73,9.55
h,10,1420621172,533.89,25.95
h,11,1420621754,2.68,2.72
h,12,0,0.00,0.00