如何组合以下两个命令:
find . -print0 | grep -z pattern | tr '\0' '\n'
find . -print0 | grep -z pattern | xargs -0 my_command
Run Code Online (Sandbox Code Playgroud)
进入单一管道?如果我不需要NUL分隔符,那么我可以这样做:
find . | grep pattern | tee /dev/tty | xargs my_command
Run Code Online (Sandbox Code Playgroud)
我想避免使用这样的临时文件:
find . -print0 | grep -z pattern > tempfile
cat tempfile | tr '\0' '\n'
cat tempfile | xargs -0 my_command
rm tempfile
Run Code Online (Sandbox Code Playgroud)
这个问题是对这些答案的后续跟进:
1)使用/ dev/tty显示中间管道结果:
https://unix.stackexchange.com/a/178754/8207082
2)使用NUL分隔的文件列表:
编辑使用my_command而不是command.
后续问题:
您可以将tee更改为指向proc sub,然后在那里执行完全相同的操作.
find . -print0 | grep -z pattern | tee >(tr '\0' '\n' > /dev/tty) | xargs -0 command
Run Code Online (Sandbox Code Playgroud)
以这种方式使用tee的唯一问题是,如果xargs命令也打印到屏幕,那么所有输出都可能混乱,因为管道和进程子都是异步的.