假设我们有一个包含时间、房间号和温度值的csv输入文件。我们必须在shell脚本中打印每个房间的最高温度,而不使用awk。我已经尝试了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
`
1条答案
按热度按时间kwvwclae1#