简体   繁体   中英

Taking averages from a CSV file bash scripting

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.

Heres the table - 在此处输入图片说明

and here is my code so far - 在此处输入图片说明

#! /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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM