Parse log file for errors and fetch variable number of lines before and after error
I have a log file containing data and error information from multiple sources whose only error report similarity is the word ERROR. When I hit this word I need to check the info after this on the same line against an error list and then retrieve lines prior to and after this line depending on the error matched.
e.g. 404 2 1
500 3 0
for error 404 get 2 before and 1 after and 500 3 before and none after.
Many of these errors are strings e.g. 'Bad Gateway'
I can do this easily using grep but to achieve this I am parsing the file once for each error type.
For some I have to read back till another string is hit and this I can't do using grep.
Has anyone an idea of how I can achieve all of this using only one pass of the log file.
I presume awk is the answer but don't know where to start.
Any help would be much appreciated.