我有这样的日志文件:
...
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列kbyte值。我需要更新所有最后2列的值kB到gB。
有关转换和列号更新的任何建议(awk,sed等)吗?
答案 0 :(得分:6)
尝试这样的事情:
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