I am trying to find a way to extract a value for a Monitoring Data file and use it further. Using an SSH connection (with password) i would like to access a file which is remotely located. The in this file I would like to search for a particular sensor name. Since this file contains data from lot of sensors and from different timesteps, I would like to pick the last (latest) string and next to this string is the sensor value and I would like to copy it and write it in a local data file and use it further in the programming. I could copy the whole file from server to local but the whole file is big and the file keeps changing every hour.
Time Sensor-name Sensor-value
25-05-2018;15:24 t_amb 24.8
25-05-2018;15:24 t_room 21.2
25-05-2018;15:24 G_global 120
25-05-2018;15:25 t_amb 25
25-05-2018;15:25 t_room 21
25-05-2018;15:25 G_global 227.8
For Example in the above mentioned file which is located in an another computer which is connected via SSH port, I would just like to search for the string 't_room' and find the last occurrence of it in the remote file and write the value next to it '21' into a file which is located at a directory locally.
Edit: More or less I want to do a similar thing but instead of a bash script I want to do it in Python :
sshpass -piHEM_MT17 ssh 192.168.101.53 "keysight" | egrep "SP_t_5_roh" | cut -d" " -f4 | tail -n 1 > /run/user/1000/temperaturtest.txt
You can pipe the data to grep
to select the last line with tail
containing "t_room", then print the 3rd column separated by space using awk
.
For example, if your data is saved in log.txt:
cat log.txt | grep t_room | tail -n 1 | awk -F" " '{print $3}'