onur
onur

Reputation: 6375

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

I have log file like this:

...
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
...

Last 2 column kbyte values. I need to update all last 2 column values kB to gB.

Any suggestion(awk,sed,etc.) for convert and update with column number?

Upvotes: 1

Views: 58

Answers (1)

Kent
Kent

Reputation: 195129

try something like this:

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

output:

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

if you want to format the number a little bit:

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

Upvotes: 6

Related Questions