So I'm having trouble taking a table of values (csv) and taking the averages of the heights and weights provided. I can display and read it, but am unsure of how exactly to store each value provided.
Asked
Active
Viewed 81 times
0
-
1Bash is perhaps not the best scripting language for this, but if that's the assignment... You'll need a total variable for height and weight that are set to zero up front, and also a counter. When the loop is over, divide the totals by the counter and that's the average. See https://stackoverflow.com/questions/6348902/how-can-i-add-numbers-in-a-bash-script for how to keep a running total, and increment the counters. Awk/Python/Perl would be easier than bash though, especially with non-integer arithmetic. – Ian McGowan Mar 03 '19 at 20:29
-
1One more link to read: https://stackoverflow.com/questions/12722095/how-do-i-use-floating-point-division-in-bash – Ian McGowan Mar 03 '19 at 20:41
-
1Post some usable for testing (ie. text) sample data with the related expected output. – James Brown Mar 03 '19 at 20:54
-
1[edit] your question to provide your sample input and output as plain text so we can test a potential solution against it. – Ed Morton Mar 04 '19 at 01:29
1 Answers
-1
#! /usr/bin/sh
# get height, weight from column 4, 5
printf "Input CSV: "
read CSV
DATA_HEIGHT=$( echo $( wc -l $CSV | cut -f1 -d" " )-1 | bc )
HEIGHT=$(
tail -n +2 $CSV |
( while
read line;
do
VAR=`echo $line | cut -d "," -f4` ;
echo $VAR
done;
) | grep -oE '[0-9]+' | paste -s -d + - | bc
)
WEIGHT=$(
tail -n +2 $CSV |
( while
read line;
do
VAR2=`echo $line | cut -d "," -f5` ;
echo $VAR2
done;
) | grep -oE '[0-9]+' | paste -s -d + - | bc
)
printf "\nHeight(in): "
echo $HEIGHT/$DATA_HEIGHT | bc
printf "Weight(lbs): "
echo $WEIGHT/$DATA_HEIGHT | bc

Stef
- 87
- 9