Shell help needed

Could someone tell me how to do the below in Korn Shell or SED?

If the 1st word (i.e. 1st character to the one character before the 1st space) of a line is the same as the 1st word of the 2nd line then add the 3rd word of the 1st line and the 3rd word of the 2nd line and divide the sum of the 4th word of the 1st and 2nd line and put the result into a new file with only the 1st word and result of the division. And I want this to loop until it reachs the end of the file.

e.g. I have a file which contains 5 lines below:

AAA Unit1 60 39
AAA Unit7 30 15
BBB Unit3 80 60
CCC Unit4 50 25
CCC Unit8 90 45

I want it to output 3 lines below:

AAA 0.6
BBB 0.75
CCC 0.5

0.6 was calculated by (39+15)/(60+30) taken from 1st and second line.

Any help will be greatly appreciated.

Change the finename in the below script and test

#!/usr/bin/ksh
print "\c" > sum_divide_final
for i in `cut -f1 -d" " sum_divide.dat | uniq`
do
dividend=0
divider=0
        for j in `grep $i sum_divide.dat | cut -f3 -d" "`
        do
                (( divider = divider + j ))
        done
        for j in `grep $i sum_divide.dat | cut -f4 -d" "`
        do
                (( dividend = dividend + j ))
        done
finalresult=$(print "scale = 2; $dividend/$divider" | bc)
print "$i"" ""$finalresult" >> sum_divide_final
done

awk soln

BEGIN{
IFS=" "
OFS=" "
}
{
arr3[$1]+=$3
arr4[$1]+=$4
}
END{
for(item in arr3)
{
        arr5[item]=arr4[item]/arr3[item]
}
for(item in arr5)
{
        print item " " arr5[item]
}
}

try this one,

not thoroughly tested;

#!/usr/bin/ksh

prev="###"
sum1=0
sum2=0
while read i
do
curr=`echo $i | cut -f1 -d" "`
val1=`echo $i | cut -f3 -d" "`
val2=`echo $i | cut -f4 -d" "`

if [ \( $curr != $prev \) -a \( $prev != "###" \) ]
then
final=$(print "scale = 2; $sum2/$sum1" | bc)
echo $prev $final
sum1=0
sum2=0
fi

sum1=$(($sum1 + $val1))
sum2=$(($sum2 + $val2))

prev=$curr
done < input.file > output.file

final=$(print "scale = 2; $sum2/$sum1" | bc)
echo $prev $final >> output.file

exit 0

When i tried the awk solution,
i got the following error.. pls explain

awk: awk.sh1:14: (FILENAME=ifile3 FNR=6) fatal: division by zero attempted

Awk:

{ if ( $1 == old_word )
  { split( old_line, a )
    print $1, (a[4]+$4)/(a[3]+$3)
    old_word = old_line = ""
  }
  else
  { if ( old_line )
    { split ( old_line, a)
      print a[1], a[4]/a[3]
    }
    old_word = $1; old_line = $0
  }
}

Thanks everyone!
I haven't tested all of them yet but I have a question with Mona's code which I tested.
The code worked perfectly but I was wondering if the 2nd line of the code is necessary or not.

  print "\\c" &gt; sum\_divide_final

I guess this line is creating an empty file but it worked fine without this line (I think because it created it in the 2nd last line).

In case of when I have an input file which has more than one space between each word and I want the previous program to work for those type of input.

Could someone tell me how to make all spaces between each words to be one space by using KSH or SED commands?

e.g.
I have a file which contains 3 lines below:
AAA Unit1 60
BBB Unit3 80
CCC Unit4 50

I want it to have only one space between each word like below:
AAA Unit1 60
BBB Unit3 80
CCC Unit4 50

For supressing the spaces u can use

tr -s ' '

And if u have both tabs and spaces in ur file u can use awk instead of cut.

for ex:

instead of
cut -f3 -d" "

U can use
awk '{print $3}'

I have a file like this....

BSC5-64-1 BSC7-1-1 99.00 99.00
BSC5-64-2 BSC7-1-1 98.00 98.00
BSC7-1-1 BSC7-1-2 -1.00 -1.00
BSC7-1-1 BSC7-1-3 -1.00 -1.00
BSC5-70-1 BSC7-1-1 99.00 99.00
BSC5-70-2 BSC7-1-1 99.00 99.00
BSC7-1-1 BSC7-2-1 -1.00 0.00
BSC7-1-1 BSC7-2-2 17.00 17.00
BSC7-1-1 BSC7-2-3 0.00 4.00
BSC7-1-1 BSC7-3-1 9.00 10.00
BSC7-1-1 BSC7-3-2 34.00 27.00
BSC7-1-1 BSC7-3-3 21.00 26.00
..........................................................
..........................................................
BSC7-1-1 BSC7-4-1 19.00 13.00
BSC7-1-1 BSC7-4-2 35.00 6.00
BSC7-1-1 BSC7-4-3 14.00 23.00
BSC7-1-1 BSC7-5-1 36.00 44.00
BSC7-1-1 BSC7-5-2 44.00 23.00
BSC5-64-1 BSC7-1-1 99.00 99.00
BSC5-64-2 BSC7-1-1 98.00 98.00
i need format this file so...
if first column is BSC7-1-1, then its ok..
else move second column to first and 4-th to 3-d..
....
can anyone help me????/

This is a different question so you should really start a new thread, but anyway try this...

awk '$1!="BSC7-1-1"{$0=$2 FS $1 FS $4 FS $3}1' file1

Regarding my first question I'm using Mona's code but when I try to calculate a number bigger than 2500000000 it give the wrong answer. I guess some where around 2500000000 is the max number but is there a way to do calculations using numbers bigger than 2500000000?

e.g. Input file with below:

AAA Unit1 2500000000 500000000

using Mona's code it should output "AAA 0.2" but it outputs "AAA -0.27"

Did you find any solution for this? It works fine for me. I am getting the below output,

AAA .20
BBB .75
CCC .50

provided the input is,

AAA Unit1 60 39
AAA Unit7 30 15
AAA Unit1 2500000000 500000000
BBB Unit3 80 60
CCC Unit4 50 25