简体   繁体   English

如何将管道从查找到解压缩到grep

[英]how to pipe from find to unzip to grep

find . -name "*.ooutline" -print0 | xargs -0 -n1 unzip -c | grep  -m 10 -Eo ".{0,20}$1.{0,20}" 

".ooutline' files are zipped OmniOutliner files. I want to find them, including recursively in subdirectories; pipe the names to unzip; have unzip unzip each .ooutline file into memory; then grep the contents of the unzipped file in memory. “ .ooutline'文件是已压缩的OmniOutliner文件。我想找到它们,包括在子目录中递归地查找这些文件;通过管道将其解压缩;将每个.ooutline文件解压缩到内存中;然后将已解压缩文件的内容grep到内存中。

I haven't had much luck trying to do this with find. 我尝试用find来做这件事没有太多运气。 I do have a working bash shell script, as follows: 我确实有一个有效的bash shell脚本,如下所示:

#!/bin/bash

for file in *.ooutline; do
    if ( unzip -c "$file" |   grep --label="$file" -H  -m 10 -Eo ".{0,20}$1.{0,20}"  )  ; then
        printf "\n\n"
    fi
done

I'd like to be able to do this on one line with find. 我希望能够通过find在一行上执行此操作。 I'd appreciate any advice on this. 我对此表示感谢。 Thank you. 谢谢。

THE NEXT DAY 第二天

Progress! 进展! This works but only on the one of the files in the list. 这有效,但仅适用于列表中的文件之一。 This means the idea is right, but I have some detail wrong. 这意味着这个想法是正确的,但我有些细节是错误的。 Note: I got the find-exec-unzip idea from another post on stack: I do have filenames with spaces. 注意:我从堆栈上的另一篇文章中得到了find-exec-unzip的想法:我确实有带空格的文件名。

find . -name '*.zip' -exec sh -c 'unzip -c -d "`dirname \"{}\"`" "{}"' ';' | grep  -m 10 -Eo ".{0,20}Peter.{0,20}"

I tried this 我试过了

find . -name '*.zip' -exec sh -c 'unzip -c  "{}"' ';' | grep  -m 10 -Eo ".{0,20}Peter.{0,20}"

and it returned these results 它返回了这些结果

Version Peter sleep and wa reason they brought Peter here for this missi Dr Adkins finds Peter on his knees prayin g outside ICU. Peter的睡眠版本,是为什么他们将Peter带到这里来的原因。这名小姐Adkins博士在ICU外面跪下祈祷。 Peter is convinced Five is not go there yet. 彼得坚信五国还没有去那里。 Peter gives him a speech 彼得给他演讲

...so it's definitely returning something from one file (an analysis of a science fiction novel). ...因此,它肯定是从一个文件返回的东西(对科幻小说的分析)。

使用zipgrep您可以执行以下操作:

find . -name '*.ooutline' -print0 | xargs -0 zipgrep -Ho '.{0,20}$1.{0,20}'

Try with GNU Parallel like this, and use all your lovely Intel CPU cores in parallel: 尝试使用GNU Parallel ,并并行使用所有可爱的Intel CPU内核:

find . -name \*.ooutline -print0 | parallel -0 --tag 'unzip -c {} | grep -Eo ".{0,20}"'

The --tag tags the output lines with the filenames. --tag用文件名标记输出行。 The {} represents the input filename. {}代表输入文件名。

I believe your original find command works fine, but you forgot to place it in a script file, say script.sh and then pass the string as command line argument? 我相信您原来的find命令可以正常工作,但是您忘了将其放在脚本文件中,例如script.sh ,然后将字符串作为命令行参数传递了吗? Here are the steps I took, let me know where I'm making the wrong assumptions: 这是我采取的步骤,请让我知道在哪里做出错误的假设:

Executed commands: 执行的命令:

echo Alice Bob Peter >'Alice Bob Peter.txt'
echo Alice Alice Alice >'Alice Alice Alice.txt'
echo Alice Peter Bob >'Alice Peter Bob.txt'
for f in 'Alice Bob Peter.txt' 'Alice Alice Alice.txt' 'Alice Peter Bob.txt';do zip "${f%txt}ooutline" "$f";done
echo 'find . -name "*.ooutline" -print0 | xargs -0 -n1 unzip -c | grep  -m 10 -Eo ".{0,20}$1.{0,20}"' >script.sh
bash script.sh Peter

Output: 输出:

Archive:  ./Alice Peter Bob.ooutline
 extracting: Alice Peter Bob.txt     
Alice Peter Bob
chive:  ./Alice Bob Peter.ooutline
tracting: Alice Bob Peter.txt     
Alice Bob Peter

It sounds like you're just asking how to call unzip on each file found by find and have it's output piped to grep . 听起来您只是在问如何对find每个文件调用unzip并将其输出通过管道传递到grep Replacing unzip with cat for demonstration: cat代替unzip进行演示:

$ cat file
Wee, sleekit,
cowrin', tim'rous beastie,
Oh whit a panic's
in thy breastie

$ find . -maxdepth 1 -name file -exec sh -c 'cat "{}" | grep "whit"' \;
Oh whit a panic's

$ find . -maxdepth 1 -name file -exec cat "{}" \; | grep "whit"
Oh whit a panic's

$ find . -maxdepth 1 -name file -print0 | xargs -0 -I {} sh -c 'cat {} | grep "whit"'
Oh whit a panic's

$ find . -maxdepth 1 -name file -print0 | xargs -0 cat | grep "whit"
Oh whit a panic's

What's wrong with either of those? 这两个怎么了? I can see you tried similar approaches and you seem to be saying there's some problem with them but you didn't provide sample input or expected output so we can't tell what the problem is. 我可以看到您尝试了类似的方法,并且您似乎在说它们有问题,但是您没有提供示例输入或预期输出,因此我们无法确定问题出在哪里。 Try using a flat text file with cat instead of requiring a zip file with unzip so you can show a [mcve] in your question. 尝试将纯文本文件与cat而不是要求带解压缩的zip文件,这样您就可以在问题中显示[mcve]。

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

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