Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I grep the contents of files returned by grep?

Tags:

grep

bash

When I look for log files with an error message using grep error *log, it returns a list of logfiles

$grep error *log

Binary file out0080-2011.01.07-12.38.log matches
Binary file out0081-2011.01.07-12.38.log matches
Binary file out0082-2011.01.07-12.38.log matches
Binary file out0083-2011.01.07-12.38.log matches

However, these are text, not binary files.

I am not sure why these are considered binary, the first few lines contain the following non-error messages:

out0134
-catch_rsh /opt/gridengine/default/spool/compute-0-17/active_jobs/327708.1/pe_hostfile
compute-0-17

I would like to grep the contents of the returned files for an error message and return the names of the files with the message.

How can I grep the contents of the returned files, rather than this list of returned files, as happens with grep error *log | grep foo?

like image 501
David LeBauer Avatar asked Dec 06 '22 00:12

David LeBauer


1 Answers

Here's the answer you might be looking for:

grep -l foo $(grep -l error *.log)

-l tells grep to print filenames only; that does the first grep, then substitutes the result into the next grep's command. Alternatively, if you like xargs:

grep -l error *.log | xargs grep -l foo

which does the same thing, using xargs to call the second grep with the first grep's results as arguments.

like image 88
Cascabel Avatar answered Jan 06 '23 18:01

Cascabel