如何编写shell脚本以打印csv文件中的特定值

esyap4oy  于 2022-11-16  发布在  Shell
关注(0)|答案(1)|浏览(148)

假设我们有一个包含时间、房间号和温度值的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

`

kwvwclae

kwvwclae1#

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

相关问题