I have a logfile with 2 distinct events (among others) that I need to capture.
Each event generates a separate, dedicated line in the logfile with this format:
timestamp - PID - process - event-type - event-details
I don't care much about anything but the event-details column of the file, and the data I'm expecting to receive there, looks like this:
Example 1: { "values":{ "SPEED":"7.0" } }
Example 2: { "values":{ "CADENCE":"41" } }
I've been trying to write a shell script that would only read the last line of the logfile every time, and depending on the contents of the event-details column, redirect the resulting SPEED or CADENCE data to a specific text file (when I say resulting SPEED/CADENCE data I mean the "integer" after the SPEED":" expression for example).
So far I was able to redirect the results to two different files, but:
- I have to "tail" the logfile twice in order for the script to work and...
- ...as a result of that, I have the feeling that the second file is not being updated at the same rate as the first one...as if, for some reason, I was missing some of the CADENCE events due to the order in which the script was written.
I tried using the sleep function, and also tried to "tail" more than one line at a time to try to mitigate the lack of CADENCE update with no luck. I just keep missing CADENCE events from time to time.
A note on the logfile behavior: Looking at the log, there are 3 events that appear most of the time, and they are always logged in the same order of appearance (CADENCE, SPEED and OTHER), and from time to time there is a 4rd event. I just wanted to clarify that the missing CADENCE events have nothing to do with that "4rd" event appearance.
This is a summarized version of the script that I have currently running:
#!/bin/bash
while :
do
tail -1 logfile.txt | grep -oP '(?<=SPEED":")[0-9]+' > spd.txt
tail -1 logfile.txt | grep -oP '(?<=CADENCE":")[0-9]+' > cad.txt
done
=======UPDATE:=======
This is the complete log line and ouput expected:
Example of line 1:
Input (from logfile.txt):
03-16 21:05:28.641 2797-2842/process:Service D/WEBSOCKET: receiving: { "values":{ "Speed MPH":"3.1", "Speed KPH":"4.9", "Miles":"0.551", "Kilometers":"0.886" } }
Output (sent to spd.txt):
4.9
Example of line 2:
Input (from logfile.txt):
03-16 21:05:29.309 2797-2842/process:Service D/WEBSOCKET: receiving: { "values":{ "RPM":"27" } }
Output: (sent to cad.txt):
27