I am reading a file into an array. This file contains comma delimited data formatted like this:
16.01,1.8
20,1.84
25.01,1.9
31.52,1.93
800.1,1.99
1000,1.98
1250,1.98
16000,2
20010,2
I need to find the closest number to "1000" in the first column, and I have a working function for that. This number is then used for further processing of the input file, however I can't get this variable until the file has been processed up to that point, which means that the second part of my script only processes the array data after the "1000" point has been found.
The only way I could see to do this, was to open the file a second time with another while loop (unless there is a way to store and re-use the array content?).
My script:
#!/bin/bash
#
find1k () {
if ((980<=$freq && $freq<=1050)); then
scalevolts=$volts
fi
}
while IFS=$',' read -r -a lines; do
[[ "$lines" =~ ^#.*$ ]] && continue
freq="${lines[0]}"
volts="${lines[1]}"
freq=$(printf "%.0f\n" $freq)
if [ -z "$scalevolts" ]; then
find1k
else
# I need to loop through the entire array again from here.
#
normalised=$(echo "scale=3; ($volts/$scalevolts)"|bc -l)
echo $freq , $volts , $normalised
fi
done < $1
Is there a way to do this without having to open the file twice? (i.e. re-use the array content).
bash version is 4.2
Thanks.
normalised?