如何将(两个)管道指令的输出传递给文件?
How do I pass output of (both) piped instructions to file?
我有两个命令 cmd1 和 cmd2,其中我执行
time cmd1 | cmd2
我想得到类似
的东西
cmd1 >> file.out and {time cmd1 >> file.out} | cmd2 >> file.out
所以有人可以建议它实际上是如何完成的吗?
编辑:正如安东尼在下面的回答所暗示的那样,tee 在这里工作,但如果我写
time cmd1 |tee -a file.out | cmd2 >> file.out
然后它只将 cmd1 的输出写入 file.out,将 cmd2 的输出写入 file.out,而我还希望将 {time cmd1} 的输出写入该文件。
我正在 Ubuntu Mate 上使用 bash shell。如果时间关键字使它复杂化,请提出一些方法来计时执行并进行准确的操作。
如果我没有正确理解你的问题,你希望 cmd
的输出是
写入 file.out
和 也 用作 cmd2
的输入。对于这种情况,您可以尝试将 tee
命令(附加 -a
选项)插入到您的命令管道中:
cmd1 | tee -a file.out | cmd2 >> file.out
例子
$ printf "one\ntwo\nthree\n" | tee -a file.out | sed 's/.*/\U&/' >> file.out
$ cat file.out
one
two
three
ONE
TWO
THREE
问题编辑版本的答案
以下构造应该可以满足您的要求:
{ time cmd1; } 2>> file.out | tee -a file.out | cmd2 >> file.out
因为time
utility provided by Bash operates on the complete pipeline, curly braces are used to group these commands所以他们可以算作一个整体。注意:结束分号 (;
) 需要在右大括号之前。
cmd1
的标准输出流通过管道传输到 tee
命令,但由于 Bash 的 time
实用程序将其计时统计信息打印到 标准错误,文件描述符2
被重定向,以便计时统计信息附加到file.out
。
上一个例子的修改版本
{ time printf "one\ntwo\nthree\n"; } 2>> file.out | tee -a file.out | sed 's/.*/\U&/' >> file.out
我有两个命令 cmd1 和 cmd2,其中我执行
time cmd1 | cmd2
我想得到类似
的东西cmd1 >> file.out and {time cmd1 >> file.out} | cmd2 >> file.out
所以有人可以建议它实际上是如何完成的吗? 编辑:正如安东尼在下面的回答所暗示的那样,tee 在这里工作,但如果我写
time cmd1 |tee -a file.out | cmd2 >> file.out
然后它只将 cmd1 的输出写入 file.out,将 cmd2 的输出写入 file.out,而我还希望将 {time cmd1} 的输出写入该文件。
我正在 Ubuntu Mate 上使用 bash shell。如果时间关键字使它复杂化,请提出一些方法来计时执行并进行准确的操作。
如果我没有正确理解你的问题,你希望 cmd
的输出是
写入 file.out
和 也 用作 cmd2
的输入。对于这种情况,您可以尝试将 tee
命令(附加 -a
选项)插入到您的命令管道中:
cmd1 | tee -a file.out | cmd2 >> file.out
例子
$ printf "one\ntwo\nthree\n" | tee -a file.out | sed 's/.*/\U&/' >> file.out
$ cat file.out
one
two
three
ONE
TWO
THREE
问题编辑版本的答案
以下构造应该可以满足您的要求:
{ time cmd1; } 2>> file.out | tee -a file.out | cmd2 >> file.out
因为time
utility provided by Bash operates on the complete pipeline, curly braces are used to group these commands所以他们可以算作一个整体。注意:结束分号 (;
) 需要在右大括号之前。
cmd1
的标准输出流通过管道传输到 tee
命令,但由于 Bash 的 time
实用程序将其计时统计信息打印到 标准错误,文件描述符2
被重定向,以便计时统计信息附加到file.out
。
上一个例子的修改版本
{ time printf "one\ntwo\nthree\n"; } 2>> file.out | tee -a file.out | sed 's/.*/\U&/' >> file.out