Skip to content
Advertisement

Shell – Trying to output last portion of a logfile (Time-stamp is the separator)

I would like to read in a logfile into shell, and output the last logged event that had occurred. These logs are selenium-python automated test results that I am using in larger script. This script requires the last chunk of the log.

Here is an example of one of the last logged events from an example output file:

JavaScript

The logfile contains other logs similar to this one in chronological order. I would like to output/scrape the last entry using the timestamp above as a delimiter/cutoff threshold. I have tried using tail and regular expressions, but am not quite sure how to go about doing this. I would prefer to read the file from the end, as efficiency is important. I would like to solve this problem in shell, but a solution in Python might also be useful.

Advertisement

Answer

I’m not sure if this is what you want, but you may try:

JavaScript

This snippet reads the file “logfile.log” backwards, an print each line, until a timestamp — with the format you gave — is found. Since the lines have been printed backwards, another call for tac reorders the output.

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement