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:

2013-01-10 13:49:55

Notes: 

FAIL: runTest (__main__.changeContent)

Traceback (most recent call last):
  File "demo-about-content-pyunit-w.py", line 294, in runTest
    self.assertEqual(browser.find_element_by_id("descriptionAbout").text, "We are a business used for automated testing and pretty much boring.", 'about desc would not change')
AssertionError: about desc would not change

Ran 1 test in 40.954s>

FAILED (failures=1)

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:

tac logfile.log | while read line; do echo ${line};
    [[ "${line}" =~ [0-9]{4}(-[0-9]{2}){2} [0-9]{2}(:[0-9]{2}){2} ]] && break;
done | tac

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