How to grep for message and if found display filename?

Hi i'm new to the forum and was hoping someone could help me with the following query.

I do alot of testing and have hundreds of log files output. I have a script (someone else wrote) which finds all the passed and failed logs and puts a number in a column onto a webpage:

e.g:

		   Pass   Fail   Total
		   ----   ----   -----
Win32:
Vista:	  	      8      0       8 
XP:	              1      3       4 

The code basically runs as a bash script on a linux box with apache and displays it on a browser.

for the failed files it displays the file info. Which basically greps the last 2 lines of the file and if it has failed it displays info from the line:

tail -n 2 *.log | grep -v FAILED | grep FAIL 

above log file grep produces:

***-TESTv3.00g-XYZ-01-TEST_RESULT-1-FAIL 10/08/2010,00:24:43 (q:\runfiles\vlist.ini)

output on the browser is below:

Client	           INI File                             Time Failed At	    test Version
XYZ-01 q:\runfiles\myfiles\win32\vlist.ini 10/08/2010,00:24:43 TESTv3.00g

Now the problem i have is i haven't got enough info to find the log.

Is there a way i can get grep to also get the file name for the failed files.

so maybe a condition:
if the above tail command produced failed results then display the filename?

Try:

find . -type f -name "*.log" | xargs -i bash -c "tail -n 2 {} | grep -v FAILED | grep FAIL && echo {}"

Hi Bartus11,

Could you please explain the xargs code ?

find . -type f -name "*.log" | xargs -i bash -c "tail -n 2 {} | grep -w FAIL && echo {}"

I think instead of using grep two times we can use grep with -w option.

that worked a charm thanks!!..

Is there a way i can make the output just the filename?

find . -type f -name "*.log" | xargs -i bash -c "tail -n 2 {} | grep -v FAILED | grep FAIL && basename {}"

---------- Post updated at 07:23 AM ---------- Previous update was at 07:20 AM ----------

With xargs -i, you can use "{}" where arguments need to be passed. So here it will be substituted with same file name in two places for every file passed from "find" utility.