awk puzzle
Hi, I am struggling to write an awk script with the correct syntax and wondered if anyone knows this really well.
I have 2 columns of data like this below which I would like to addition. 229K 128M 2.41G 4G 126M 2G 2.86G 0 2.69G 4G 175M 2G 2.86G none 1. Convert none to 0 2. If mb(M) then do nothing. If kb(K), or gb(G) then convert to mb 3. Addition the 1st and 2nd column and print output in gb Code:
cat file | sed -e 's/none/0/g' | nawk -F" " '{if($1 | $2 ~ /G/) {printf "%.2f %.2f\n", $1*1024, $2} elif {if($1 | $2 ~ /K/) {printf "%.2f %.2f\n", $1/1024/1024, $2}}' else 'print $1, $2' | nawk -F" " '{sum += $1, sum2 =+ $2}END{ printf("Used : %12.fGB Quota : %12.fGB\n",sum/1024, sum2/1024)}' |
Code:
{ Code:
0.22 128.00 128.22 Besides it should work only for upper-case M and K. It can be fixed by tolower function. |
Code:
sed 's/^/((/;s/$/))\/1024000/;s/ \+/)+(/g;s/none/0/;s/K/*1024/g;s/G/*1024000/g;s/M//g' file |bc |
Hi goldenbarb, nice post thanks for the awk, that's something that I can re-use later on.
However I have to addition the total of column 1 and the total of column 2 instead of adding up column1 and column2, and outputting this into column 3. I tried using the following, but the numbers didnt add up. sum1+=$arr1 sum2+=$arr2 |
Hi,
Building on goldenbarb's code from post #2: Code:
#!/bin/bash The bold parts are the additions and/or changes. Hope this helps. |
Code:
{ Code:
0.22 128.00 |
All times are GMT -5. The time now is 08:11 AM. |