why cant I redirect the output from sed to a file

隐身守侯 提交于 2021-02-20 18:51:12

问题


I am trying to run the following command

./someprogram | tee /dev/tty | sed 's/^.\{2\}//' > output_file

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.

Is there any way that I can redirect the output from sed in this command to a file?


回答1:


Remove output-buffering from sed command using the -u flag and make sure what you want to log isn't on stderr

-u, --unbuffered

  load minimal amounts of data from the input files and flush the output buffers more often

Final command :

./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 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.




回答2:


You got a Stderr & stdout problem. Checkout In the shell, what does " 2>&1 " mean? on this topic. Should fix you right up.



来源:https://stackoverflow.com/questions/23026371/why-cant-i-redirect-the-output-from-sed-to-a-file

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!