我如何grep由grep返回的文件的内容? [英] How do I grep the contents of files returned by grep?

查看:500
本文介绍了我如何grep由grep返回的文件的内容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我使用 grep error * log 查找包含错误消息的日志文件时,它会返回一个日志文件列表

  $ grep错误* log 

二进制文件out0080-2011.01.07-12.38.log匹配
二进制文件out0081-2011.01.07-12.38 .log匹配
二进制文件out0082-2011.01.07-12.38.log匹配
二进制文件out0083-2011.01.07-12.38.log匹配

但是,这些是文本,而不是二进制文件。



我不确定为什么这些被认为是二进制的,

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

我想grep 返回的文件的内容为一个错误消息并返回与消息的文件的名称。

如何使用 grep error * log | grep来返回返回的文件的内容,而不是返回的文件的列表。 grep foo

解决方案

以下是您可能要查找的答案:

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

-l <​​/ code>告诉grep只打印文件名;执行第一个grep,然后将结果替换为下一个grep的命令。或者,如果您喜欢xargs:

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

可以做同样的事情,使用xargs调用第一个grep grep的结果作为参数。


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?

解决方案

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.

这篇关于我如何grep由grep返回的文件的内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆