linux - Averaging lines without awk using bash -
i'm starting use bash, , i'm having difficulties bash arithmetic. lets have cities 3 temperatures, , i'd average temperatures without using awk command. how can this? i've done awk, i'm practicing different techniques.
# citytemp.txt 4 cities, 3 day's temperatures toronto 20 25 30 miami 80 80 110 london 40 20 60 new york 5 10 15
lets i'm trying write script output:
25 toronto 90 miami 40 london 10 new york
i've done piping , awk command, i'm having problems doing without using awk.
again, i'm new this. tried loop, didn't know doing.
---edit 1:
benjamin w. i'm editing again, right i'm playing loop:
#!/bin/bash in $(cat $1) echo "i is: ${i}" done < $1
this is, maybe obviously, printing every field citytemp.txt 1 @ time.
---edit 2:
this ending attempt
while read -a rows total=0 sum=0 in "${rows[@]}" sum=`expr $sum + $i` total=`expr $total + 1` done average=`expr $sum / $total` output = ${average} done < $1 echo ${averages}
this should work in posix shell, may shebang #!/bin/sh.
#!/bin/sh avg () { city= while [ $# -gt 3 ]; city="$city $1" shift done echo $((($1 + $2 + $3) / 3)) $city } while read line; avg $line done < citytemp.txt
this have been lot easier if city last on each line, lol.
Comments
Post a Comment