Linux 在子目录中递归搜索 .bz2 文件中的文本字符串

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

Linux search text string from .bz2 files recursively in subdirectories

linuxrecursiongrepbzip2

提问by Semu

I have a case where multiple .bz2 files are situated in subdirectories. And I want to search for a text, from all files, using bzcat and grep command linux commands.

我有一个案例,其中多个 .bz2 文件位于子目录中。我想使用 bzcat 和 grep 命令 linux 命令从所有文件中搜索文本。

I am able to search one-one file by using the following command:

我可以使用以下命令搜索一对一文件:

bzcat <filename.bz2> | grep -ia 'text string' | less

But I now I need to do the above for all files in subdirectories.

但是我现在需要对子目录中的所有文件执行上述操作。

采纳答案by hek2mgl

You can use bzgrepinstead of bzcatand grep. This is faster.

您可以使用bzgrep代替bzcatgrep。这更快。

To grep recursively in a directory tree use find:

要在目录树中递归地 grep 使用find

find -type f -name '*.bz2' -execdir bzgrep "pattern" {} \;

findis searching recursively for all files with the *.bz2extension and applies the command specified with -execdirto them.

find正在递归搜索所有具有*.bz2扩展名的文件,并将指定的命令应用于-execdir它们。

回答by Igor Chubin

There are several methods:

有几种方法:

bzgrep regexp $(find -name \*.bz2)

This method will work if number of the found files is not very big (and they have no special characters in the pathes). Otherwise you better use this one:

如果找到的文件数量不是很大(并且路径中没有特殊字符),则此方法将起作用。否则你最好使用这个:

find -name \*.bz2 -exec bzgrep regexp {} /dev/null \;

Please note /dev/nullin the second method. You use it to make bzgrepprint the filename, where the regexpwas found.

请注意/dev/null在第二种方法中。您可以使用它来bzgrep打印找到的文件名regexp

回答by BMW

Continuous your code with fixes by bzcat:

使用 bzcat 修复继续您的代码:

find . -type f -name "*.bz2" |while read file
do 
    bzcat $file | grep -ia 'text string' | less
done

回答by Anton Shtin

Just try to use:

尝试使用:

bzgrep --help
grep through bzip2 files

Usage: bzgrep [grep_options] pattern [files]

用法: bzgrep [grep_options] pattern [files]

For example, I need grep information from list of files by number 1941974:

例如,我需要从编号为 1941974 的文件列表中获取 grep 信息:

'billing_log_1.bz'
'billing_log_2.bz'
'billing_log_3.bz'
'billing_log_4.bz'
'billing_log_5.bz'

What can I do?

我能做什么?

bzgrep '1941974' billing_log_1

bzgrep '1941974' billing_log_1