awk math and csv output

Hi I have this list

 
592;1;Z:\WB\DOCS;/FS3_100G/FILER112/BU/MPS/DOCS;;;;\\FILER112\BUMPS-DOCS\;580,116,544,878 Bytes;656,561 ;77,560 
592;2;Z:\WB\FOCUS;/FS3_100G/FILER112/BU/MPS/FOCUS;;;;\\FILER112\BUMPS-FOCUS\;172,430 Bytes;6 ;0 
896;3;Z:\WB\FUNDONFUND;/FS3_100G/FILER112/BU/MPS/FUNDONFUND;;;;\\FILER112\BUMPS-FOFUND\;0 Bytes;0 ;0 
592;4;Z:\WB\MONARCH;/FS3_100G/FILER112/BU/MPS/MONARCH;;;;\\FILER112\BUMPS-MON\;14,531,579 Bytes;137 ;3 
931;5;S:\X\DIST\DV;/FS7_100G/FILER104/BU-D/DIST/DV;;;;\\FILER104\BUDIST-DV\;452,928,819 Bytes;10 ;2

Delimiter semicolon ;
Field #9 is the folder size

I would like to have the same list instead of bytes converted to GB

I tried to run

 
awk -F ";" '{print $9/1024/1024/1024}' file

but it does not work.
Help is appreciated, it could also be in perl.

First remove all the strings and , from the $9 .

use

awk -F ";" '{ gsub("[A-z]","",$9);gsub(",","",$9); print $9/(1024*1024*1024)}' file
1 Like
awk -F\; '{gsub(/,/,"",$9);$9=($9/1024/1024/1024) " GB"}1' CONVFMT='%.6f' OFS=\; file
1 Like

Try like..

awk -F ";" '{print $9+0/(1024*1024*1024)}' test.txt 

That will not work. 9th field has commas too. That will make awk treat $9 as a number upto the first comma.

Sorry ,i didn't notice..

Thank you this is worked perfectly but I have a question.
Is it possible to keep the $9 field with the size in bytes, and to add a new field in GB?

I tried this and it works, but is there a simpler way to do it?
Windows awk

awk -F ";" "{ gsub(\"[A-z]\",\"\",$9);gsub(\",\",\"\",$9); print $1\";\"$2\";\"$3\";\"$4\";\"$9\"Bytes\"\";\" $9/(1024*1024*1024)\" GB\"}" CONVFMT="%.6f" file
 
awk -F ";" '{ gsub("[A-z]","",$9);gsub(",","",$9); print $1";"$2";"$3";"$4";"$9"Bytes"";" $9/(1024*1024*1024)" GB"}' CONVFMT='%.6f' s.final.csv>s.final.gb.csv

I don't know why you are using double quotes..

try

awk -F ";" '{ gsub("[A-z]","",$9);gsub(",","",$9); $9=$9" Bytes "($9/(1024*1024*1024))" GB"}1' CONVFMT="%.6f" OFS=";" file

Like this?

awk -F\; '{t=$9;gsub(/,/,"",t);$9=$9 " (" (t/1024/1024/1024) " GB)"}1' CONVFMT='%.6f' OFS=\; file
1 Like

Why not use

awk -F\; '{t=$9;gsub(/,/,"",t);$9=$9 " (" (t/1073741824) " GB)"}1' CONVFMT='%.6f' OFS=\; file

? Would save two divisions per input line.

:)...that's just to show what is intended to be done...
come on, be nice (at least this nice) to the others who'll be reading/maintaining the script and don't happen to see the "GB" (or don't know what a GB is!!!).
But, yes, your suggestion is a good one.

Thank you again

$9=($9/(1024^3)) 

this will work too

Yes, you are right, but exponentiation is even more CPU intensive than multiplication.

1 Like