问题描述
我正在尝试运行以下命令
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
命令中删除输出缓冲,并确保要执行的操作日志不在stderr上
Remove output-buffering from sed
command using the -u
flag and make sure what you want to log isn't on stderr
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
在流中会发生这种情况(通常是程序在其整个生命周期内将输出发送到stdout).
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重定向到文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!