Grep only for lines of process that caused the error

greptext processing

There is a service running as multiple processes and the logs of all come to one huge log (don't ask me why). It looks like this:

process1: bla bla bla
process2: ha ha ha
process3: tarara **error_has_happened**
process3: details of the error here
process1: bla bla bla
process3: more details of the error here
process2: ha ha ha

The error can happen in any of the processes so I want my greps to find this for all processes:

process3: tarara **error_has_happened**
process3: details of the error here
process3: more details of the error here

so basically detect the error_has_happened and the process in which it happened and print the next N lines only from that process. This should happen for all the processes that had this error, assuming the logs of details of the error from different processes are not interleaved

Is it possible to do this using grep alone? how?

Best Answer

You can do it with:

grep "error_has_happened" -A3 logfile.log

Where 3, is the number of lines after error_has_happened appearence that will be shown. However, this will also show other process outputs not only the outputs of the process which sends the error.

A more elaborated command that worked for me in a quick test is:

grep "error_has_happened" logfile.log | cut -d : -f1 | sort -u |
  while IFS= read -r process; do
    grep "^$process:" logfile.log |
      grep -A3 "error_has_happened"
  done
Related Question