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

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

问题描述

这不工作。这可以在find中完成吗?或者我需要xarg吗?

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).

在您的示例中,您可以选择使用顶层shell来执行管道:

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'\n" | 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.)

在效率方面,查找,一次调用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找到-exec命令?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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