我如何包含管道 |在我的 linux find -exec 命令中? [英] How do I include a pipe | in my linux find -exec command?

查看:20
本文介绍了我如何包含管道 |在我的 linux find -exec 命令中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这不起作用.这可以在查找中完成吗?还是我需要 xargs?

This isn't working. Can this be done in find? Or do I need to xargs?

find -name 'file_*' -follow -type f -exec zcat {} | agrep -dEOE 'grep' ;

推荐答案

将管道符号解释为运行多个进程的指令并将一个进程的输出通过管道传输到另一个进程的输入的工作是 shell 的职责(/bin/sh 或等效的).

The job of interpreting the pipe symbol as an instruction to run multiple processes and pipe the output of one process into the input of another process is the responsibility of the shell (/bin/sh or equivalent).

在您的示例中,您可以选择使用顶级外壳来执行管道,如下所示:

In your example you can either choose to use your top level shell to perform the piping like so:

find -name 'file_*' -follow -type f -exec zcat {} ; | agrep -dEOE 'grep'

就效率而言,这个结果花费了一次 find 调用、多次 zcat 调用和一次 agrep 调用.

In terms of efficiency this results costs one invocation of find, numerous invocations of zcat, and one invocation of agrep.

这将导致只产生一个 agrep 进程,该进程将处理由多次调用 zcat 产生的所有输出.

This would result in only a single agrep process being spawned which would process all the output produced by numerous invocations of zcat.

如果你出于某种原因想多次调用 agrep,你可以这样做:

If you for some reason would like to invoke agrep multiple times, you can do:

find . -name 'file_*' -follow -type f 
    -printf "zcat %p | agrep -dEOE 'grep'
" | sh

这会构造一个使用管道执行的命令列表,然后将它们发送到新的 shell 以实际执行.(省略最后的| sh"是调试或执行像这样的命令行试运行的好方法.)

This constructs a list of commands using pipes to execute, then sends these to a new shell to actually be executed. (Omitting the final "| sh" is a nice way to debug or perform dry runs of command lines like this.)

就效率而言,此结果花费了一次 find 调用、一次 sh 调用、多次 zcat 调用和多次 agrep 调用.

In terms of efficiency this results costs one invocation of find, one invocation of sh, numerous invocations of zcat and numerous invocations of agrep.

就命令调用次数而言,最有效的解决方案是 Paul Tomblin 的建议:

The most efficient solution in terms of number of command invocations is the suggestion from Paul Tomblin:

find . -name "file_*" -follow -type f -print0 | xargs -0 zcat | agrep -dEOE 'grep'

... 花费一次 find 调用、一次 xargs 调用、几次 zcat 调用和一次 agrep 调用.

... which costs one invocation of find, one invocation of xargs, a few invocations of zcat and one invocation of agrep.

这篇关于我如何包含管道 |在我的 linux find -exec 命令中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆