1

I'm trying to manipulate a TCP stream using gawk. The TCP server streams out a text string separated by whitespaces. I want to add an extra string at the beginning and then separate the remaining fields with commas instead of whitespaces. I've tried as below:

socat TCP4:127.0.0.1:8888 - | gawk '{out=""; for(i=1;i<=NF;i++){if (i > 1) {out=out","$i} else out="Hello,"$i}; print out}'

which is working. But now I want to redirect that flow outside on a second socket like below:

socat TCP4:127.0.0.1:5754 PIPE gawk '{out=""; for(i=1;i<=NF;i++){if (i > 1) {out=out","$i} else out="$LZER0,POS,"$i}; print out}' | socat - TCP:127.0.0.1:9999

But nothing at 127.0.0.1:9999 I've tried to listen with:

socat tcp-listen:9999,reuseaddr,ignoreeof,fork stdout

but nothing.

I've made another test:

socat TCP4:127.0.0.1:5754 PIPE gawk '{out=""; for(i=1;i<=NF;i++){if (i > 1) {out=out","$i} else out="$LZER0,POS,"$i}; print out}' | socat - -

but again nothing on my stdout.

Can you help me?

0

It is likely that the output of gawk is buffered when its output goes to a pipe and that there's not enough output generated to flush the buffer.

To flush the output buffer manually, use fflush() in the gawk program:

gawk '{ ... ; print out; fflush() }'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.