Computing data in awk

Hello,
I am a newbie in programing. I want to compute the following in awk.
I have the following data file:

ID Value1 Value2 Value3
sade 0.21 0.45 23
foly 0.31 0.34 43
jude 0.40 0.11 63
jude 0.53 0.32 34
sade 0.67 0.49 66
foly 0.30 0.20 56

I want to take an ID �sade� , then take its value3 which is 23 and divide it with sum of value3 (23+66=89) like 23/89=0.258. then multiply it with value1 and value2 and then sum it.
For sade:
res=23/89
L1[ID]=L1[ID]+(res * 0.21)
L2[ID]=L2[ID]+(res * 0.45)

Now for the second �sade� record
res=66/89
L1[ID]=L1[ID]+(res * 0.67)
L2[ID]=L2[ID]+(res * 0.49)

Similar calculations for other IDs (jude,foly) as well.
Thank you so much for your help.
Regards,
Ubee

For clarity can you post desired result. It will help to understand the logic.

---------- Post updated at 01:11 PM ---------- Previous update was at 12:57 PM ----------

Hmm! 23+66=89 in my logic.

awk '{
 v1[$1] += $1
 v2[$1] += $2
 v3[$1] += $3
}
END {
for ( x in v1 )
  print x, v1[x], v3[x], v3[x] 
}' "$file"

ripat! thanks for the correction.I was in hurry. :slight_smile:
For each of the ID I want the following output:

L1[sade]=(resi * 0.21)+ (resj * 0.67) where resi =23/89 and resj =66/89
L2[sade]=(resi * 0.45)+ (resj * 0.49)

L1[foly]=( resi * 0.31)+ (resj * 0.30) where resi =43/99 and resj =56/99
L2[foly]=( resi * 0.34)+ (resj * 0.20)

L1[jude]=( resi * 0.40)+ (resj * 0.53) where resi =63/97 and resj =34/97
L2[jude]=( resi * 0.11)+ (resj * 0.32)

I hope you got the logic.
Thanks a lot.
Regards,
Ubee

---------- Post updated at 04:09 PM ---------- Previous update was at 03:23 PM ----------

cfajohnson !
In this code you didn't consider the value of "res" which is for "sade, res=23/89 and 66/89". Similarly for jude and foly. Please check my last message. I hope you will get my point.
Thank you.

Is this what you are after?

awk '
NR==FNR{tot[$1]+=$4;l2[$1]=$2;l3[$1]=$3;l4[$1]=$4;next}
{
	print sprintf("L1[%s]=%f\nL2[%s]=%f\n",
	$1, ($4/tot[$1]*$2)+(l4[$1]/tot[$1]*l2[$1]),
	$1, ($4/tot[$1]*$3)+(l4[$1]/tot[$1]*l3[$1]))
}' yourFile yourFile

On the sample file given above, it produces this output:

L1[sade]=0.551124
L2[sade]=0.479663

L1[foly]=0.304343
L2[foly]=0.260808

L1[jude]=0.445567
L2[jude]=0.183608

L1[jude]=0.371546
L2[jude]=0.224330

L1[sade]=0.993708
L2[sade]=0.726742

L1[foly]=0.339394
L2[foly]=0.226263

Bravo!!! :slight_smile:
It is working perfectly.
Thank you ripat and cfajohnson for your time.
I appreciate your cooperation.

Regards,
Ubee

my %hash;
open FH,"<a";
while(<FH>){
 chomp;
 my @tmp=split;
 push @{$hash{$tmp[0]}->{val}}, $_;
 $hash{$tmp[0]}->{sum}+=$tmp[3];
}
close FH;
foreach my $key(keys %hash){
 my @tmp=@{$hash{$key}->{val}};
 for(my $i=0;$i<=$#tmp;$i++){
   my @t=split(" ",$tmp[$i]);
   print $hash{$key}->{sum};
   print $tmp[$i]," ",$t[1]*$t[3]/$hash{$key}->{sum}," ",$t[2]*$t[3]/$hash{$key}->{sum},"\n";
 }
}