假设我们有一个输入csv文件,其中包含关于时间、房间号和温度值的数据。我们必须在不使用awk的情况下,在shell脚本中为每个房间打印相关房间的最高温度。我已经为5个房间尝试了以下代码,但我不知道是否有N个房间如何做到这一点。我想使代码可重复使用。请帮忙。
`
max_val1=0
max_val2=0
max_val3=0
max_val4=0
max_val5=0
#Using loop to read the file and save it as column
while IFS=, read -r Time Room Value
do
if [ $Room == "Room1" ] #Comparing
then
if [ $Value -gt $max_val1 ]; then
max_val1=$Value
fi
fi
if [ $Room == "Room2" ]
then
if [ $Value -gt $max_val2 ]; then
max_val2=$Value
fi
fi
if [ $Room == "Room3" ]
then
if [ $Value -gt $max_val3 ]; then
max_val3=$Value
fi
fi
if [ $Room == "Room4" ]
then
if [ $Value -gt $max_val4 ]; then
max_val4=$Value
fi
fi
if [ $Room == "Room5" ]
then
if [ $Value -gt $max_val5 ]; then
max_val5=$Value
fi
fi
done< <(tail -n+2 data.csv)
echo "Room 1 ,"$max_val1
echo "Room 2 ,"$max_val2
echo "Room 3 ,"$max_val3
echo "Room 4 ,"$max_val4
echo "Room 5 ,"$max_val5
`
max_value()
{
max=0
while IFS=, read -r Time Room Value
do
if [ "$Room" == "Room$1" ]; then
if [ $Value -gt $max ]; then
max=$Value
fi
fi
done< <(tail -n+2 data.csv)
echo "Room"$1 ", Max room temp is "$max
}
echo "Enter the max no of rooms"
read end
for(( i=1; i <= $end; i++ ))
do
max_value $i
done