在文件夹中的 gzip 文件中查找字符串 [英] find string inside a gzipped file in a folder
问题描述
我目前的问题是我有大约 10 个文件夹,其中包含 gzip 文件(平均每个大约 5 个).这使得它可以打开和查看 50 个文件.
My current problem is that I have around 10 folders, which contain gzipped files (around on an average 5 each). This makes it 50 files to open and look at.
是否有更简单的方法来确定文件夹中的 gzip 压缩文件是否具有特定模式?
Is there a simpler method to find out if a gzipped file inside a folder has a particular pattern or not?
zcat ABC/myzippedfile1.txt.gz | grep "pattern match"
zcat ABC/myzippedfile2.txt.gz | grep "pattern match"
我可以在一行中为所有文件夹和子文件夹执行相同的操作,而不是编写脚本吗?
Instead of writing a script, can I do the same in a single line, for all the folders and sub folders?
for f in `ls *.gz`; do echo $f; zcat $f | grep <pattern>; done;
推荐答案
zgrep 将查看 gzipped 文件,有一个 -R 递归选项,和一个 -H 显示文件名选项:
zgrep will look in gzipped files, has a -R recursive option, and a -H show me the filename option:
zgrep -R --include=*.gz -H "pattern match" .
操作系统特定的命令,因为并非所有参数都适用:
OS specific commands as not all arguments work across the board:
Mac 10.5+: zgrep -R --include=*.gz -H 模式匹配".
Ubuntu 16+: zgrep -i -H 模式匹配"*.gz
这篇关于在文件夹中的 gzip 文件中查找字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!