bash 递归地grep zip文件列表中的模式

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

grep a pattern in list of zip files recursively

bashunixcommand-linegrep

提问by Vijay

I am using the following command on command line for getting the pattern matched lines.

我在命令行上使用以下命令来获取模式匹配的行。

find . -name "*.gz"|xargs gzcat|grep -e "pattern1" -e "pattern2"

i need now to find only the file names where the pattern is present. how can i do it on command line?

我现在只需要找到存在模式的文件名。我怎样才能在命令行上做到这一点?

grel -lhas no use since i am using xargs gzcatbefore grep

grel -l没用,因为我xargs gzcat以前用过grep

采纳答案by Timofey Stolbov

for i in $(find . -name "*.gz"); do gzcat $i|grep -qe "n1" -e "n2" && echo $i; done

回答by Vijay

Check if you have zgrep available. And then, if yes:

检查您是否有可用的 zgrep。然后,如果是:

find . -name '*.gz' -exec zgrep -l -e ".." -e ".." {} +

If you don't have it - well, just copy it from some machine that has it (all linuxes I use have it by default) - it's a simple bash script.

如果您没有它 - 好吧,只需从拥有它的机器上复制它(我使用的所有 linux 默认都有它) - 这是一个简单的 bash 脚本。

回答by hmontoliu

Untested; does everything inside find so if you have loads of gz files you wont have performance problems as runs each gzcat/grep as soon as it finds files nothing is piped out:

未经测试;里面的所有东西都找到了,所以如果你有大量的 gz 文件,你不会有性能问题,因为一旦找到文件就运行每个 gzcat/grep 没有任何东西被管道输出:

find . -iname '*.gz' -exec bash -c 'gzcat  | grep -q -e "pattern1" -e "pattern2" && echo ' {} {} \;

回答by Ned

In bash, I'd do something like this (untested):

在 bash 中,我会做这样的事情(未经测试):

find . -name '*.gz' | while read f ; do gzcat $f | grep -q -e "pattern1" -e "pattern2" && echo $f ; done

回答by kenorb

ripgrep

ripgrep

Use ripgrep, for example, it's very efficient, especially for large files:

使用ripgrep,例如,它非常有效,尤其是对于大文件:

rg -z -e "pattern1" -e "pattern2" *.gz

or:

或者:

rg -z "pattern1|pattern2" .

or:

或者:

rg -zf pattern.file .

Where pattern.fileis a file containing all your patterns separated by a new line character.

pattern.file包含由换行符分隔的所有模式的文件在哪里。

-z/--search-zipSearch in compressed files (such as gz, bz2, xz, and lzma).

-z/--search-zip在压缩文件中搜索(如gzbz2xz,和lzma)。

回答by kenorb

grep/zgrep/zegrep

grep/ zgrep/zegrep

Use zgrepor zegrepto look for pattern in compressed files using their uncompressed contents (both GNU/Linux and BSD/Unix).

使用zgrep或使用zegrep压缩文件的未压缩内容(GNU/Linux 和 BSD/Unix)在压缩文件中查找模式。

On Unix, you can also use grep(which is BSD version) with -Z, including -zon macOS.

在 Unix 上,您还可以使用grepBSD 版本)和-Z,包括-z在 macOS 上。

Few examples:

几个例子:

zgrep -E -r "pattern1|pattern2|pattern3" .
zegrep "pattern1|pattern2|pattern3" **/*.gz
grep -z -e "pattern1" -e "pattern2" *.gz # BSD/Unix only.

Note: When you've globbing option enabled, **checks the files recursively, otherwise use -r.

注意:当您启用 globbing 选项时**递归检查文件,否则使用-r.

-R/-r/--recursiveRecursively search subdirectories listed.

-E/--extended-regexpInterpret pattern as an extended regular expression (like egrep).

-Z(BSD), -z/--decompress(BSD/macOS) Force grep to behave as zgrep.

-R/ -r/--recursive递归搜索子目录上市。

-E/ 将--extended-regexp模式解释为扩展的正则表达式(如egrep)。

-Z( BSD), -z/ --decompress( BSD/macOS) 强制 grep 表现为zgrep.