Tuesday, January 4, 2022

[SOLVED] How do I grep starting at a specific point in a log file?

Issue

A have daily syslog files which contain syslog messages in the format: MMM DD HH:MM:SS additional_data_here

We make a change, then want to see if the syslog messages continue.

For example, say a change was made at 09:55. I want to ignore everything prior to the first line that contains Oct 29 09:55:00. Then I want to grep for my error message after that first line match.

For this example, I have to create several different statements, like this:

grep -e "Oct 29 09:5[5-9]" syslog20211029 | grep "[my message]"

grep -e "Oct 29 1[0-1]:" syslog20211029 | grep "[my message]"

But I do this often enough that I'd like to find a better, more consistent way. Something like:

start-at-first-match "Oct 29 09:55:00" syslog20211029 | grep "[my message]"

But I don't know what the start-at-first-match option is. Any suggestions?


Solution

If you want to restrict yourself to using grep, you can't really but with the option -A num it can still meet your need (giving a big number for num) :

grep -A 10000000 "Oct 29 09:55:00" syslog20211029

This will print the matching line and the next 10 million.

If you want everything that follows the line for sure (without having to give an "unreachable" number of lines), you have to use another command (like sed or awk). Using sed: sed -n '/Oct 29 09:55:00/,$ p' (with -n won't print the lines by default, and from the line you want, between /pattern/, to the end of file $ you ask sed to print the lines).



Answered By - syme