为什么从管道读取时awk会做全缓冲
我正在从连接到发送nmea字符串的gps设备的串行端口读取。 一个简化的调用来说明我的观点: $ awk '{ print $0 }' /dev/ttyPSC9 GPGGA,073651.000,6310.1043,N,01436.1539,E,1,07,1.0,340.2,M,33.3,M,,0000*56 $GPGSA,A,3,28,22,09,27,01,19,17,,,,,,2.3,1.0,2.0*39 $GPRMC,073651.000,A,6310.1043,N,01436.1539,E,0.42,163.42,070312,,,A*67 GPGGA,073652.000,6310.1043,N,01436.1540,E,1,07,1.0,339.2,M,33.3,M,,0000*55 $GPGSA,A,3,28,22,09,27,01,19,17,,,,,,2.3,1.0,2.0*39 如果我改为尝试从管道读取,awk会将输入发送到stdout之前先对其进行缓冲。 $ cat /dev/ttyPSC9 | awk '{ print $0 }' <long pause> GPGGA,073651.000,6310.1043,N,01436.1539,E,1,07,1.0,340.2,M,33.3,M,,0000*56 $GPGSA,A,3,28,22,09,27,01,19,17,,,,,,2.3,1.0,2.0*39 $GPRMC,073651.000,A,6310.1043,N,01436.1539,E,0.42,163.42,070312,,,A*67 GPGGA,073652.000,6310.1043,N,01436.1540,E,1,07,1.0,339.2,M,33.3,M,,0000*55 $GPGSA,A,3,28,22,09,27,01,19,17,,,,,,2.3,1.0,2.0*39 如何避免缓冲? 编辑:凯尔·琼斯(Kyle Jones)建议,猫正在缓冲它的输出,但是这似乎没有发生: $ strace cat /dev/ttyPSC9 | awk '{ print $0 }' write(1, "2,"..., 2) = 2 read(3, …