1

Given a file with an input in the format of (longitude,latitude,date,time,temperature), write a bash script that returns the place and time of the highest measured temperature on the list.

Example input:

53.0382,96.5753,2010.11.16.,07:23,38
53.0382,96.5753,2000.06.21.,09:05,-16
53.0382,96.5753,2007.05.16.,02:00,-4
53.0382,96.5753,2008.07.27.,22:38,-6
53.0382,96.5753,2001.07.09.,09:50,-12
53.0382,96.5753,2016.12.08.,22:55,28

Example output:

The highest measured temperature was 38 degrees on 2010.11.16. at 07:23. it was measured at the coordinates of 53.0382, 96.5753

I've written a script that successfully takes the input and splits it into different arrays for each of the different values given. I was trying to loop through the temperatures to find the index of the highest one, using it to index the date,time,and location arrays for the output.

#!/bin/bash
latitude=(); longitude=(); date=(); time=(); value=();
while IFS=, read -ra arr;
do
    latitude+=(${arr[0]})
    longitude+=(${arr[1]})
    date+=(${arr[2]})
    time+=(${arr[3]})
    value+=(${arr[4]})
done < temperatures.txt
max=0
maxI=0
count=$(wc -l <temperatures.txt)
for ((i=0; i<$count; i++)) ;
do
echo ${value[i]}
    if ((${value[i]} > $max)) ; then
        max=${value[i]}
        maxI=$i
    fi
done
echo $max
echo $maxI

With the above code, I get the error syntax error: invalid arithmetic operator (error token is " > 0"). It seems to be a problem with line 17, the if statement. I'd appreciate it if anyone could shed some light on what my problem is.

4
  • Btw.: I suggest to avoid variable names which are shell builtins or keywords. Here time. Commented Nov 14, 2021 at 17:28
  • change your debug to echo i=$i and \${value[$i]}=${value[i] and you may see what your problem is. Good luck.` Commented Nov 14, 2021 at 17:29
  • The code looks ok I think. Check temperatures.txt for malformed field 5, such as an extra comma, space or trailing dot. Commented Nov 14, 2021 at 17:41
  • this code works for me and generates max=38 ; maxI=0, so perhaps an issue with original code or data file? from a debugging point of view, add set -xv (enable debug mode) at the beginning of the script, run the script, review the output for unexpected variable assignments; alternatively, cut-n-paste your actual code into shellcheck.net to see if there are any syntax issues (with your actual code) Commented Nov 14, 2021 at 17:45

3 Answers 3

2

Skip the array and just keep track of the highest temp (and associated values), eg;

temp=-1000

while IFS=, read -r a b c d e
do
    [[ "${e}" -gt "${temp}" ]] &&
    long="${a}"                &&
    lat="${b}"                 &&
    tdate="${c}"               &&
    ttime="${d}"               &&
    temp="${e}"
done < temperatures.txt

printf "The highest measured temperature was %s degrees on %s at %s. It was measured at the coordinates of %s, %s\n" "${temp}" "${tdate}" "${ttime}" "${long}" "${lat}"

This generates:

The highest measured temperature was 38 degrees on 2010.11.16. at 07:23. It was measured at the coordinates of 53.0382, 96.5753

For processing a large volume of rows I'd probably opt for something like awk (for performance reasons), eg:

awk -F, '
$5 > temp { long=$1; lat=$2; tdate=$3; ttime=$4; temp=$5 }
END       { printf "The highest measured temperature was %s degrees on %s at %s. It was measured at the coordinates of %s, %s\n", temp, tdate, ttime, long, lat }
' temperatures.txt
Sign up to request clarification or add additional context in comments.

Comments

1

I don't get the error you are speaking about. Try:

#!/bin/bash
latitude=(); longitude=(); date=(); time=(); value=()
while IFS=, read -ra arr;
do
    latitude+=(${arr[0]})
    longitude+=(${arr[1]})
    date+=(${arr[2]})
    time+=(${arr[3]})
    value+=(${arr[4]})
    (( count++ ))
done < temperatures.txt
max=-100
maxI=0
for ((i=0; i<$count; i++)) ;
do
    if ((${value[i]} > $max)) ; then
        max=${value[i]}
        maxI=$i
    fi
done
echo -n "The highest measured temperature was ${value[$maxI]} degrees"
echo -n " on ${date[$maxI]} at ${time[$maxI]}."
echo -n " It was measured at the coordinates of"
echo " ${latitude[$maxI]}, ${longitude[$maxI]}"

Instead of invoking wc -l for counting the number of input lines, which implies to read the whole input file a second time, I just increment the counter each time an input line is read.

I initialize max to -100 in order to make this script work in case of all negative temperatures.

Comments

1

Not a solution to your bug, but a better solution to the problem.

sort -nt, -k5,5 temperatures.txt |
tail -n 1 |
awk -F , '{print ("The highest measured temperature was "$5 \
           " degrees on "$3" at "$4". It was measured at the" \
           " coordinates of "$1", "$2".")}'

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.