I am interested in learning how to extract information
- count occurrences of keywords,
- get the timestamp for specific occurrences of keywords (note that the timestamps will always for the same day; usually, within a couple of hours of the same day),
- get the elapsed time of specific log entries
from a text log file (log.txt) via a script (Linux bash or Windows batch or Python). All this information should be written in another text file (results.txt) or be printed on the terminal.
Basically, all the other log entries (i.e. with the blah blah are ignored).
For example for the following text log file, where each line starts with a timestamp followed by an empty space, a dash line(-) and one or more empty space(s) followed by keywords:
11:59:35.875 - action - WRITE(34) start
11:59:35.875 - blah blah
11:59:35.875 - blah blah
11:59:35.877 - blah blah
11:59:35.897 - KEYWORD_1
11:59:35.975 - action - WRITE(34) end
11:59:36.992 - KEYWORD_1
11:59:36.999 - KEYWORD_1
11:59:37.535 - blah blah
11:59:37.545 - ACTION_A - STATE: type 2
11:59:37.575 - blah blah
11:59:37.577 - blah blah
11:59:37.845 - KEYWORD_2
11:59:37.945 - ACTION_B result
11:59:37.950 - blah blah
11:59:38.075 - action - WRITE(22) start
11:59:38.075 - blah blah
11:59:38.085 - blah blah
11:59:38.097 - KEYWORD_2
11:59:39.975 - action - WRITE(22) end
Firstly, I would like to count the occurrences of each of the KEYWORD_1 and the KEYWORD_2 (e.g. 2 and 2, respectively).
Secondly, I want to be able to print the timestamps of each KEYWORD occurrence, e.g. 11:59:35.897 for the first occurrence of the KEYWORD_1.
Finally, find the elapsed time between two log entries:
- those who start with
- action - WRITE(#) startand end with- action - WRITE(#) endwhere#is any integer number, e.g. 11:59:35.975 - 11:59:35.875 =1msfor the firstWRITE(34) - those who start with
- ACTION_A ...and end withACTION_B ...e.g. 11:59:37.545 - 11:59:37.945 =4msfor the firstACTION_A .. ACTION_B.
I have tried find /c "KEYWORD_1" log.txt >results.txt (Windows batch) to count the occurrences but I cannot extract the respective timestamp. For the other requirements I have no idea how to start as I have no experience with such actions before. Tried adapting the answers from this question for my needs with no success.
Any code fragment example or link with related resources will be much appreciated.