从CSV文件bash脚本获取平均值

时间:2019-03-03 20:10:25

标签: bash shell scripting

因此,我无法获取一个数值表(csv)以及所提供的高度和重量的平均值。我可以显示和读取它,但是不确定如何准确存储所提供的每个值。

在此放置表格-enter image description here

这是到目前为止的代码- enter image description here

1 个答案:

答案 0 :(得分: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