Linux 使用先前 grep 的结果进行 grep
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14735511/
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
grepping using the result of previous grep
提问by Sonoman
Is there a way to perform a grep based on the results of a previous grep, rather than just piping multiple greps into each other. For example, say I have the log file output below:
有没有一种方法可以根据先前 grep 的结果执行 grep,而不仅仅是将多个 grep 相互连接。例如,假设我有下面的日志文件输出:
ID 1000 xyz occured
ID 1001 misc content
ID 1000 misc content
ID 1000 status code: 26348931276572174
ID 1000 misc content
ID 1001 misc content
To begin with, I'd like to grep the whole log file file to see if "xyz occured" is present. If it is, I'd like to get the ID number of that event and grep through all the lines in the file with that ID number looking for the status code.
首先,我想 grep 整个日志文件文件以查看是否存在“xyz发生”。如果是,我想获取该事件的 ID 号,并使用该 ID 号查找文件中的所有行以查找状态代码。
I'd imagined that I could use xargs or something like that but I can't seem to get it work.
我以为我可以使用 xargs 或类似的东西,但我似乎无法让它工作。
grep "xyz occured" file.log | awk '{ print }' | xargs grep "status code" | awk '{print $NF}'
Any ideas on how to actually do this?
关于如何实际执行此操作的任何想法?
采纳答案by Andy Ross
You're almost there. But while xargs can sometimes be used to do what you want (depending on how the next command takes its arguments), you aren't actually using it to grep for the ID you just extracted. What you need to do is take the output of the first grep (containing the ID code) and use that in the next grep's expression. Something like:
您快到了。但是,虽然 xargs 有时可用于执行您想要的操作(取决于下一个命令如何获取其参数),但您实际上并没有使用它来 grep 刚刚提取的 ID。您需要做的是获取第一个 grep 的输出(包含 ID 代码)并在下一个 grep 的表达式中使用它。就像是:
grep "^ID `grep 'xyz occured' file.log | awk '{print }'` status code" file.log
Obviously another option would be to write a script to do this in one pass, a-la Ed's suggestion.
显然,另一种选择是编写一个脚本来一次性完成此操作,这是 Ed 的建议。
回答by Ed Morton
Just use awk:
只需使用awk:
awk '{info[] = info[] awk '/status code/{code[]=$NF} /xyz occured/{ids[]} END{ for (id in ids) print code[id]}' file.log
ORS} /xyz occured/{ids[]} END{ for (id in ids) printf "%s",info[id]}' file.log
or:
或者:
ID 1000 xyz occured
ID 1001 misc content
ID 1000 misc content
ID 1000 status code: 26348931276572174
ID 1000 misc content
ID 1001 misc content
depending what you really want to output. Some expected output in your question would help.
取决于您真正想要输出的内容。您问题中的一些预期输出会有所帮助。
回答by Eric Leschinski
Grep the result of a previous Grep:
Grep 前一个 Grep 的结果:
Given this file contents:
鉴于此文件内容:
grep "xyz" file.log | awk '{ print }' > f.log; grep `cat f.log` file.log;
This command:
这个命令:
ID 1000 xyz occured
ID 1000 misc content
ID 1000 status code: 26348931276572174
ID 1000 misc content
returns this:
返回这个:
for x in `grep "xyz occured" file.log | cut -d\ -f2`
do
grep $x file.log
done
It looks for "xyz" in file.log places the result in f.log. Then greps for that ID in file.log. If the outer grep returns multiple ID numbers, then the inner grep will only search the first ID number and error out on the others.
它在 file.log 中查找 "xyz" 将结果放在 f.log 中。然后在 file.log 中查找该 ID。如果外层 grep 返回多个 ID 号,则内层 grep 将只搜索第一个 ID 号并在其他 ID 号上出错。
回答by madflojo
Yet another way
又一种方式
grep $x file.log >> /var/tmp/$x.out
The thing I like about this method is if you wanted to you could write the output to a file for each status code.
我喜欢这种方法的一点是,如果您愿意,可以将每个状态代码的输出写入文件。
grep 'patten1' *.txt | grep 'pattern2'
回答by B.Kocis
回答by yucer
This is all about retrieve the files in a narrowed search scope. In your case the search scope is determined by a file content.
这完全是关于在缩小的搜索范围内检索文件。在您的情况下,搜索范围由文件内容决定。
I have found this problem more often while reducing the search scope through many searches (applying filters to the previous grep results).
我经常发现这个问题,同时通过多次搜索缩小搜索范围(对以前的 grep 结果应用过滤器)。
Trying to find general answer:
试图找到一般答案:
1) Generate a list with the result of the first grep:
1)用第一个grep的结果生成一个列表:
xargs grep -i pattern
2) Second grep into the list of files like here
2)第二个grep进入文件列表,如here
grep 'pattern1' | awk -F':' '{print }' | xargs grep -i 'pattern2'
3) apply this cascading filter the times you need just addding awk to get only the filenames and xargs
to pass the filenames to grep -i
3) 应用此级联过滤器的时间,您只需添加 awk 即可仅获取文件名并将xargs
文件名传递给 grep -i
For example:
例如:
##代码##