Hey i have another query . I am trying to add all odd and even columns of the percentages calculated and adding them as columns to the left of the percentages. so in the above percenages calculated totoal odd would be ($1+$3....= 45.83+51.61...) same thing for even as well

o

riginal input
55 65 48 45 48 68 32 68 44 34 88 65
82 63 52 54 51 68 75 0 0 20 10 77
55 77 60 55 22 60 40 25 75 55 45 90
20 80 33 63 0 64 32 22 75 0 43 56
54 54 12 35 48 87 65 12 77 85 0 15



calulated input
45.83 54.17 51.61 48.39 41.38 58.62 32.00 68.00 56.41 43.59 57.52 42.48
56.55 43.45 49.06 50.94 42.86 57.14 100.00 0.00 0.00 100.00 11.49 88.51
41.67 58.33 52.17 47.83 26.83 73.17 61.54 38.46 57.69 42.31 33.33 66.67
20.00 80.00 34.38 65.62 0.00 100.00 59.26 40.74 100.00 0.00 43.43 56.57
50.00 50.00 25.53 74.47 35.56 64.44 84.42 15.58 47.53 52.47 0.00 100.00



sample output:
t(odd) T(even)
313.28 315.22 45.83 54.16 51.61 48.38 41.37 58.62 32.00 68.00 ......
259.94 380.98 56.55 43.44 49.05 50.94 42.85 57.14 100.00 0

eggi found the solution to find the percentages of the original input (thanks again).....I was wondering if anyone could help me with this one..
can we use an arrary like this ???

array[j]
for (i = o,i <2000;,i++)
print i+2
for (j =1; j<2000)
print j+2
but i need to embed into the code :
awk 'BEGIN{lasti=1}{for (i=1;i<=NF;i++) {if ( i%2==0 ) {y=$lasti+$i;printf("%.2f %.2f ", $lasti/y*100, $i/y*100)}lasti=i }printf"\n"  }'

PS: i need to embed the calulation code into any of this code.. THANK YOU IN ADVANCE

I also found a ksh code that does similar thing:

$ cat calc
#! /usr/bin/ksh


bc |&
print -p scale=2
exec < sample
while read line ; do
set -- $line
while (($#)) ; do
if (($1 + $2)) ; then
print -p "$1 * 100 / ($1 + $2)"
read -p result1
print -p "$2 * 100 / ($1 + $2)"
read -p result2
echo $result1 $result2 \\c
shift 2
else
echo 00.00 00.00 \\c
fi
done
echo
done
exit 0
$
$
$ ./calc

Edited 3 Years Ago by happygeek: fixed formatting

hey there,

Glad I could help out on the last one. For this you can run:

$ awk 'BEGIN{sum1=$1;sum2=$2}{for (i=1;i<=NF;i++) {if ( i%2 ) {sum1 += $i; printf"%.2f ", $i}else {sum2 += $i; printf"%.2f ", $i}}}' FILE

to get this output:

45.83 54.17 51.61 48.39 41.38 58.62 32.00 68.00 56.41 43.59 57.52 42.48 56.55 43.45 49.06 50.94 42.86 57.14 100.00 0.00 0.00 100.00 11.49 88.51 41.67
58.33 52.17 47.83 26.83 73.17 61.54 38.46 57.69 42.31 33.33 66.67 20.00 80.00 34.38 65.62 0.00 100.00 59.26 40.74 100.00 0.00 43.43 56.57 50.00 50.00
25.53 74.47 35.56 64.44 84.42 15.58 47.53 52.47 0.00 100.00

If you ever want to get a grand total of the even and odd columns, keep this one handy ;)

$ awk 'BEGIN{sum1=$1;sum2=$2}{for (i=1;i<=NF;i++) {if ( i%2 ) sum1 += $i;else sum2 += $i}}END{print sum1,sum2}' slink
1318.05 1681.95

Best wishes,

Mike

Thank you...

If you had to divide all the odd columns by the sum of the odd colums and even by the sum of even columns.. how would you do it.. ?

like 45.83/1318.05, 54.17/1681.95 51.61/1318.05 48.39/1681.05....

something like this?
if (($3 / $1)) ; then
print -p "$3/$1)"
read -p result1
print -p "$4 / $2)"
read -p result2
output="${output}$(echo $result1 $result2//c"
shift 2
else
output="${output}$(echo 00.00 00.00 \\c)"

Hey there,

Glad to help - you're stretching my awk-mind - good exercise :)

I would do this to multiply each odd by the total of all odds (and each even by the total of all evens) on each line, like this (I changed the printf decimal notation to give you 4 digits to the right of the decimal, since if you leave it at 2, you get a lot of 0.03's, etc - if that's what you want, you can just change it back):

$ awk 'BEGIN{odds=1318.05;evens=1681.95}{for (i=1;i<=NF;i++) {if ( i%2 ) {odd=$i/odds; printf"%.4f ", odd}else {even=$i/evens; printf"%.4f ", even}}}
' FILE

which would produce this:

0.0348 0.0322 0.0392 0.0288 0.0314 0.0349 0.0243 0.0404 0.0428 0.0259 0.0436 0.0253 0.0429 0.0258 0.0372 0.0303 0.0325 0.0340 0.0759 0.0000 0.0000 0.0
595 0.0087 0.0526 0.0316 0.0347 0.0396 0.0284 0.0204 0.0435 0.0467 0.0229 0.0438 0.0252 0.0253 0.0396 0.0152 0.0476 0.0261 0.0390 0.0000 0.0595 0.0450
0.0242 0.0759 0.0000 0.0330 0.0336 0.0379 0.0297 0.0194 0.0443 0.0270 0.0383 0.0640 0.0093 0.0361 0.0312 0.0000 0.0595

Keep on keeping on - hope this helping you learn, although your questions aren't trivial. This stuff confuses me at a quick glance ;)

Take it easy,

Mike

Hey Again,

As an addendum. If you wanted to pass values from your first awk run to this one, instead of harcoding the variables in the BEGIN block, you could do this instead (note that you're echoing the result of the previous query - to grab the grand total of odds and evens - in backticks and feeding that to "while read":

$ echo `awk 'BEGIN{sum1=$1;sum2=$2}{for (i=1;i<=NF;i++) {if ( i%2 ) sum1 += $i;else sum2 += $i}}END{printf"%.4f %.4f", sum1,sum2}' FILE`|while read
x y;do awk -v odds=$x -v evens=$y '{for (i=1;i<=NF;i++) {if ( i%2 ) {odd=$i/odds; printf"%.4f ", odd}else {even=$i/evens; printf"%.4f ", even}}}' FILE;done

and get the same thing:

0.0348 0.0322 0.0392 0.0288 0.0314 0.0349 0.0243 0.0404 0.0428 0.0259 0.0436 0.0253 0.0429 0.0258 0.0372 0.0303 0.0325 0.0340 0.0759 0.0000 0.0000 0.0
595 0.0087 0.0526 0.0316 0.0347 0.0396 0.0284 0.0204 0.0435 0.0467 0.0229 0.0438 0.0252 0.0253 0.0396 0.0152 0.0476 0.0261 0.0390 0.0000 0.0595 0.0450
0.0242 0.0759 0.0000 0.0330 0.0336 0.0379 0.0297 0.0194 0.0443 0.0270 0.0383 0.0640 0.0093 0.0361 0.0312 0.0000 0.0595

Cheers,

Mike

hmm.. interesting.. ok after using the first code for calculating the percentages and sum of odd and even colums here the output:
t(odd) t(even) ...percentages
284.73 315.22 45.83 54.16 51.61 48.38 41.37 58.62 32.00 68.00 56.41 43.58 57.51 42.48
259.94 340.02 56.55 43.44 49.05 50.94 42.85 57.14 100.00 0 0 100.00 11.49 88.50
273.20 326.74 41.66 58.33 52.17 47.82 26.82 73.17 61.53 38.46 57.69 42.30 33.33 66.66
257.05 342.92 20.00 80.00 34.37 65.62 0 100.00 59.25 40.74 100.00 0 43.43 56.56
243.02 356.94 50.00 50.00 25.53 74.46 35.55 64.44 84.41 15.58 47.53 52.46 0 100.00

so your are essentially removing the percengaes again.. but this time.. dividing all the odd colums by the t(odd) column and even columns by t(even)
this should eventually give out this kind of output

16.09 17.18 18.12 15.34 .....

dividng $3/$1 , $4/$2, $5/$1, $6/$2......................

{
for(i=1;i<=NF;i++)
{ if(i%2)
printf("%.2f ",$i*100/($i+$(i+1)))
else
printf("%.2f ",$i*100/($i+$(i-1)))
}
print ""
}

this however is not giving me the right output..

No problem :)

For this last case, you can just simplify your code and it will give you the output you listed.

{
for(i=3;i<=NF;i++)
{ if(i%2)
printf("%.2f ",$i*100/$1)
else
printf("%.2f ",$i*100/$2)
}
print ""
}

against this number set

284.73 315.22 45.83 54.16 51.61 48.38 41.37 58.62 32.00 68.00 56.41 43.58 57.51 42.48
259.94 340.02 56.55 43.44 49.05 50.94 42.85 57.14 100.00 0 0 100.00 11.49 88.50
273.20 326.74 41.66 58.33 52.17 47.82 26.82 73.17 61.53 38.46 57.69 42.30 33.33 66.66
257.05 342.92 20.00 80.00 34.37 65.62 0 100.00 59.25 40.74 100.00 0 43.43 56.56
243.02 356.94 50.00 50.00 25.53 74.46 35.55 64.44 84.41 15.58 47.53 52.46 0 100.00

will give you these results

16.10 17.18 18.13 15.35 14.53 18.60 11.24 21.57 19.81 13.83 20.20 13.48
21.76 12.78 18.87 14.98 16.48 16.80 38.47 0.00 0.00 29.41 4.42 26.03
15.25 17.85 19.10 14.64 9.82 22.39 22.52 11.77 21.12 12.95 12.20 20.40
7.78 23.33 13.37 19.14 0.00 29.16 23.05 11.88 38.90 0.00 16.90 16.49
20.57 14.01 10.51 20.86 14.63 18.05 34.73 4.36 19.56 14.70 0.00 28.02

Cheers,

Mike

No problem,

Glad to help out - plus I get to learn, too :)

Best wishes,

Mike

This article has been dead for over six months. Start a new discussion instead.