问题描述
我正在尝试运行以下命令
I am trying to run the following command
./someprogram | tee /dev/tty | sed 's/^.{2}//' > output_file
但是当我去检查文件时,文件总是空白的.如果我删除 >output_file
从命令末尾,我可以看到 sed 的输出,没有任何问题.
But the file is always blank when I go to check it. If I remove > output_file
from the end of the command, I am able to see the output from sed without any issues.
有什么方法可以将此命令中 sed 的输出重定向到文件?
Is there any way that I can redirect the output from sed in this command to a file?
推荐答案
使用 -u
标志从 sed
命令中删除输出缓冲,并确定你想要什么日志不在标准错误上
Remove output-buffering from sed
command using the -u
flag and make sure what you want to log isn't on stderr
-u, --无缓冲
load minimal amounts of data from the input files and flush the output buffers more often
最终命令:
./someprogram | tee /dev/tty | sed -u 's/^.{2}//' > output_file
流会发生这种情况(通常是程序在其整个生命周期内将输出发送到标准输出).
This happens with streams (usually a program sending output to stdout during its whole lifetime).
sed
/grep
和其他命令在这些情况下会进行一些缓冲,您必须明确禁用它才能在程序仍在运行时进行输出.
sed
/ grep
and other commands do some buffering in those cases and you have to explicitly disable it to be able to have an output while the program is still running.
这篇关于为什么我不能将输出从 sed 重定向到文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!