I'm trying to calculate averages over a given range specified in one file and applying it to numbers in a different file. I am having trouble finding examples in bash where you can use information from 2 separate files at once. Here's what I am trying to do:
The first file has specified ranges that I want averages for:
ranges.txt
Sc0 1 5
Sc1 69 72
The second file contains the numbers I need to take the averages from (using the 3rd column):
allNumbers.txt
Sc0 1 30
Sc0 2 40
Sc0 3 40
Sc0 4 50
Sc0 5 10
Sc0 6 30
Sc1 69 40
Sc1 70 10
Sc1 71 20
Sc1 72 30
Here's what I'd like to have: averages.txt
34
25
I am trying to do this in the bash loop shown below, but I am fairly new to bash scripting and this code is not working.
#!/bin/bash
count=0;
total=0;
while read rangeName rangeStart rangeStop #make column variables for range.txt
while read name position sum #make column variables for allNumbers.txt
while [$rangeName == $name && $rangeStart < $position <= $rangeStop]; do
for i in $sum; do
total=$(echo $total+$i | bc)
((count++))
done
echo "$total / $count" | bc #print out averages
done
done < allNumbers.txt
done < ranges.txt
Can someone help me out with this? Thanks in advance.