I'm just starting to use bash, and I'm having some difficulties with bash arithmetic. Lets say I have a some cities with three temperatures, and I'd like to average the temperatures without using the awk command. How can I do this? I've done it with awk, but I'm practicing with different techniques.
# cityTemp.txt with four cities, three day's temperatures
Toronto 20 25 30
Miami 80 80 110
London 40 20 60
New York 5 10 15
Lets say I'm trying to write a script to output:
25 Toronto
90 Miami
40 London
10 New York
I've already done this with some piping and the awk command, but I'm having problems doing this without using awk.
Again, I'm new to this. I tried a for loop, but I didn't really know what I was doing.
---edit 1:
Benjamin W. I'm editing again, but right now I'm playing with a loop:
#!/bin/bash
for i in $(cat $1)
do
echo "i is: ${i}"
done < $1
This is, maybe obviously, just printing every field from cityTemp.txt one at a time.
---edit 2:
This was my ending attempt
while read -a rows
do
total=0
sum=0
for i in "${rows[@]}"
do
sum=`expr $sum + $i`
total=`expr $total + 1`
done
average=`expr $sum / $total`
Output = ${average}
done < $1
echo ${averages}