bash zgrep 目录中的多个 gz 文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21549038/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 09:27:45  来源:igfitidea点击:

zgrep multiple gz files in directory

bashgrep

提问by Mithrand1r

I have problem executing zgrepcommand for looking for wordin the directory where I have almost 1000 *.gzfiles.

我在执行zgrep命令以word在我有近 1000 个*.gz文件的目录中查找时遇到问题。

I am trying with:

我正在尝试:

find /var/www/page/logs/ -name "*.gz" -exec zgrep '/index.php' {} \;

result is:

结果是:

GET : '/index.php'
GET : '/index.php'
GET : '/index.php'

And it works.I get list of occurance of index.phpwith no file name where it was found. It is kind of useless for me unless i knew in which files (filename) it appears.

它有效。我得到index.php了没有文件名的出现列表。除非我知道它出现在哪个文件(文件名)中,否则它对我来说没什么用。

How can i fix it?

我该如何解决?

回答by devnull

You could supply the -Hoption to list the filename:

您可以提供-H列出文件名的选项:

find /var/www/page/logs/ -name "*.gz" -exec zgrep -H '/index.php' {} \;

If you wanted only the list of matching files, use -l:

如果您只想要匹配文件的列表,请使用-l

find /var/www/page/logs/ -name "*.gz" -exec zgrep -l '/index.php' {} \;

回答by Mark Setchell

Make grep search in two files, then it will tell you which one it found it in:

在两个文件中进行 grep 搜索,然后它会告诉你它在哪个文件中找到的:

find /var/www/page/logs/ -name "*.gz" -exec zgrep '/index.php' {} /dev/null \;

And it won't take long to search in /dev/null.

在 /dev/null 中搜索不会花费很长时间。

Here is an example without /dev/null:

这是一个没有 /dev/null 的例子:

zgrep chr your.gz
>chrMCHU - Calmodulin - Human, rabbit, bovine, rat, and chicken
>chrgi|5524211|gb|AAD44166.1| cytochrome b [Elephas maximus maximus]

and with /dev/null

和 /dev/null

zgrep chr your.gz /dev/null
your.gz:>chrMCHU - Calmodulin - Human, rabbit, bovine, rat, and chicken
your.gz:>chrgi|5524211|gb|AAD44166.1| cytochrome b [Elephas maximus maximus]