How to grep the contents of files returned by grep?

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

$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 files, not binary files.

I'm not sure why they are considered binary, the first few lines contain the following messages without error:

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 the error message and return the file names with the message.

How can I grep the contents of the returned files and not this list of returned files, how does this happen grep error *log | grep foo?

+3
source share
4 answers

Here is the answer you can find:

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

-l grep, ; grep, grep. , xargs:

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

, xargs grep grep .

+7

-a, --text
         , ;          -binary-files = text.

grep -a "some error message" *.log

Btw, grep

, , , . TYPE               ...

Update

, foo , , :

grep -la "error.*foo" *.log < - foo

+6

I'm doing it.

$ find. -type f -name * .log | fgrep -v [all undesirable] | xargs grep -i [search inside files]

+2
source

Comment on how only grep for foo in the files corresponding to the error, you can:

for i in *log ; do
    grep -a error $i >/dev/null 2>&1 && {
        echo -e "File $i:\n\t"
        grep -a foo $i
    }
done
0
source

Source: https://habr.com/ru/post/1784584/


All Articles