awk如何立即刷新缓冲区,不缓存完整输出(更频繁地刷新标准输出)

stevehe 2023年05月05日 106次浏览

解决方案

方案一:

  cat /dev/ttyPSC9 | awk '{ print $0; system("")}'

方案二:

cat /dev/ttyPSC9 | stdbuf --output=L awk '{print $0}'

附:

sed 可以使用 -u 选项更频繁地刷新缓冲区

参考来源:

https://unix.stackexchange.com/questions/33650/why-does-awk-do-full-buffering-when-reading-from-a-pipe
https://unix.stackexchange.com/questions/25372/turn-off-buffering-in-pipe

Google搜索:

awk unbuffered output
https://www.google.com/search?q=awk+unbuffered+output&newwindow=1&sxsrf=APwXEddtlc3wKLwN2Y_DgaJ5YqFFdA5aXg%3A1683234264070&ei=2B1UZJj3A57JkPIPwfemmA4&oq=awk+unbuffered+&gs_lcp=Cgxnd3Mtd2l6LXNlcnAQARgBMgQIABAeMgQIABAeOgoIABBHENYEELADSgQIQRgAUNAOWNAOYL4ZaAFwAXgAgAGeAogBngKSAQMyLTGYAQCgAQHIAQXAAQE&sclient=gws-wiz-serp