Read/parse text file

Okay, I’m switching things up a bit after reading another post on the board about the exec binding. I have since cleaned up my bash script.

I can call this script from a rule (and hopefully eventually from the exec binding), pass it an argument (i.e. I want to retrieve the 12th field) and populate an item in OpenHAB. This script reads my two-line txt file (which is really a CSV file), skips the first line and only reads the second line. I used this page as a reference for writing this script:

linesToSkip=1
intWx=$1
{
for ((i=$linesToSkip;i--;)) ;do
    read
    done
while read line ;do
   # echo $line
   fields=($(printf "%s" "$line"|cut -d"," --output-delimiter=" " -f1-))
   echo ${fields[intWx]}
done
} < /mnt/wxlogs/scriptlog.txt

This is properly reporting values now when running it from a rule as suggested in the aforementioned post.

I’m debating now whether to stick with running the script and updating my Davis weather items via a rule or using the exec binding to update the item. I will have 10 - 15 items to define. In your experience, which is the better way to go? I’m still not getting how to handle the formatting of the exec binding in my items file.

Thanks for putting up with me!

Brian

1 Like