Why can't I pass stdout from netcat to awk?

800 views Asked by At

I'm using Raspbian (actually it's HASSBian) on a RPi3. I'm trying to parse output from netcat:

pi@raspberrypi:~ $ netcat localhost 1099 | grep -i on
01/01 22:57:29 Rx RF HouseUnit: C2 Func: On
01/01 22:57:30 Rx RF HouseUnit: B4 Func: On
01/01 22:57:31 Rx RF HouseUnit: B5 Func: On
01/01 22:57:32 Rx RF HouseUnit: B6 Func: On
01/01 22:57:40 Rx RF HouseUnit: C1 Func: On
01/01 22:57:47 Rx RF HouseUnit: C2 Func: On
01/01 22:58:03 Rx RF HouseUnit: C2 Func: On
01/01 22:58:04 Rx RF HouseUnit: C1 Func: On

but when I try to pipe this to awk to pull out the two fields I want I get no output. It looks like this:

pi@raspberrypi:~ $ netcat localhost 1099 | grep -i on | awk '{print $6,$8}'

Am I wrong in thinking that this should just work?

pi@raspberrypi:~ $ uname -a
Linux raspberrypi 4.4.38-v7+ #938 SMP Thu Dec 15 15:22:21 GMT 2016 armv7l GNU/Linux
1

There are 1 answers

5
nos On

The output of grep is fully buffered when not writing to a terminal. Use the --line-buffered flag for grep

netcat localhost 1099 | grep --line-buffered -i on | awk '{print $6,$8}'

For programs that does not allow you to control its output buffering behavior, it the program can be wrapped with the stdbuf command