简体   繁体   English

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

[英]Linux search text string from .bz2 files recursively in subdirectories

I have a case where multiple .bz2 files are situated in subdirectories. 我有一个案例,其中多个.bz2文件位于子目录中。 And I want to search for a text, from all files, using bzcat and grep command linux commands. 我想使用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. 但我现在需要对子目录中的所有文件执行上述操作。

You can use bzgrep instead of bzcat and grep . 您可以使用bzgrep而不是bzcatgrep This is faster. 这更快。

To grep recursively in a directory tree use find : 要在目录树中递归grep,请使用find

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

find is searching recursively for all files with the *.bz2 extension and applies the command specified with -execdir to them. find以递归方式搜索具有*.bz2扩展名的所有文件,并将指定了-execdir的命令应用于它们。

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). 如果找到的文件数量不是很大(并且它们在pathes中没有特殊字符),则此方法将起作用。 Otherwise you better use this one: 否则你最好使用这个:

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

Please note /dev/null in the second method. 请注意第二种方法中的/dev/null You use it to make bzgrep print the filename, where the regexp was found. 你用它来使bzgrep打印文件名,找到正则regexp

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM