如何显示NUL分隔数据的中间管道结果? [英] How can I display intermediate pipeline results for NUL-separated data?
问题描述
如何组合以下两个命令:
How can I combine the following two commands:
find . -print0 | grep -z pattern | tr '\0' '\n'
find . -print0 | grep -z pattern | xargs -0 my_command
进入单个管道?如果不需要NUL分隔符,则可以执行以下操作:
into a single pipeline? If I don't need NUL separators then I can do:
find . | grep pattern | tee /dev/tty | xargs my_command
我想避免使用这样的临时文件:
I want to avoid using a temporary file like this:
find . -print0 | grep -z pattern > tempfile
cat tempfile | tr '\0' '\n'
cat tempfile | xargs -0 my_command
rm tempfile
这个问题是这些答案的后续内容:
This question is a follow-up to these answers:
1)使用/dev/tty显示中间管道结果:
1) Using /dev/tty to display intermediate pipeline results:
https://unix.stackexchange.com/a/178754/8207082
2)使用NUL分隔的文件列表:
2) Using a NUL-separated list of files:
https://stackoverflow.com/a/143172/8207082
已修改为使用 my_command
而不是 command
.
后续问题:
在子shell内写入/dev/tty的Makefile规则?
推荐答案
您只需将tee更改为指向proc sub,然后在其中进行完全相同的操作即可.
You can just change the tee to point to proc sub, then do the exact same thing in there.
find . -print0 | grep -z pattern | tee >(tr '\0' '\n' > /dev/tty) | xargs -0 command
以这种方式使用tee的唯一问题是,如果xargs命令也打印到屏幕上,则由于管道和处理子都是异步的,因此所有输出都有可能变得混乱.
The only issue with using tee this way, is that if the xargs command also prints to screen, then it is possible for all the output to get jumbled since both the pipe and process sub are asynchronous.
这篇关于如何显示NUL分隔数据的中间管道结果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!