bash 我如何 grep 由 grep 返回的文件的内容?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4650026/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I grep the contents of files returned by grep?
提问by David LeBauer
When I look for log files with an error message using grep error *log, it returns a list of logfiles
当我使用 查找带有错误消息的日志文件时grep error *log,它会返回一个日志文件列表
$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 ofthe returned files for an error message and return the names of the files with the message.
我想 grep返回文件的内容以获取错误消息,并返回带有消息的文件名。
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?
我怎样才能 grep 返回文件的内容,而不是这个返回文件列表,就像grep error *log | grep foo?
回答by Cascabel
Here's the answer you might be looking for:
以下是您可能正在寻找的答案:
grep -l foo $(grep -l error *.log)
-ltells 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:
-l告诉 grep 只打印文件名;执行第一个 grep,然后将结果替换为下一个 grep 的命令。或者,如果您喜欢 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.
它做同样的事情,使用 xargs 以第一个 grep 的结果作为参数调用第二个 grep。
回答by SiegeX
-a, --text
Process a binary file as if it were text; this is equivalent to the --binary-files=text option.
-a, --text 像
处理文本一样处理二进制文件;这等效于 --binary-files=text 选项。
grep -a "some error message" *.log
grep -a "some error message" *.log
Btw, here is how grep determines binary from text files
顺便说一句,这里是 grep 如何从文本文件中确定二进制文件
If the first few bytes of a file indicate that the file contains binary data, assume that the file is of type TYPE. By default, TYPE is binary...
如果文件的前几个字节表明该文件包含二进制数据,则假定该文件的类型为 TYPE。默认情况下,TYPE 是二进制...
Update
更新
If you want just a list of file names which contain the word foowithin the line that also contains errorthen you can do one or the other of these:
如果您只想要一个文件名列表,其中在包含错误的行中包含单词foo,那么您可以执行以下一项或多项操作:
grep -la "error.*foo" *.log<-- assumes foocomes after error
grep -la "error.*foo" *.log<-- 假设foo出现在错误之后
回答by Prashanth Sundaram
I do this.
我这样做。
$find . -type f -name *.log | fgrep -v [anything unwanted] | xargs grep -i [search inside files]
$查找。-type f -name *.log | fgrep -v [任何不需要的] | xargs grep -i [在文件中搜索]
回答by Jé Queue
A comment asked about how to only grep for foo in the files that match the error, you can:
一条评论询问如何在与错误匹配的文件中仅 grep 为 foo ,您可以:
for i in *log ; do
grep -a error $i >/dev/null 2>&1 && {
echo -e "File $i:\n\t"
grep -a foo $i
}
done

