Here is my grep string to print only the last occurrence, with before and after lines. Note that the tail Argument is sum of B and A args + 1, so that it prints the data of only the last 1 match. Now I need to print last 2 such matches. I thought of doubling the tail arg like 5+5+1 (For -- line), for ex: tail -11 should return last 2 matches, but I want it to be strict (I guess its tail -maxNumber, but I want it to be something like tail -MinNumber), so it has to either print 2 occurrences or nothing. Right now even if there is only one sample, it returns the output, I want output only if there are at least 2 matches. May be we can pipe it to awk to solve this. Please help.
grep -h -B 1 -A 3 'Authentication Error' MyLogFile.txt | tail -5
Output is:
line1 Content
Authentication Error while calling
line3 Content
line4 Content
line5 Content
If there are multiple occurrences:
grep -h -B 1 -A 3 'Authentication Error' MyLogFile.txt | tail -11
Output is:
line1 Content
Authentication Error while calling
line3 Content
line4 Content
line5 Content
--
line11 Content
Authentication Error while calling
line13 Content
line14 Content
line15 Content
But even if there is only one occurrence:
grep -h -B 1 -A 3 'Authentication Error' MyLogFile.txt | tail -11
Output is:
line1 Content
Authentication Error while calling
line3 Content
line4 Content
line5 Content