I’m using netcat and piping the output to gawk. Here is an example byte sequence that gawk will receive:
AAAA=AAAA;AAAA;AAAA=
Since this is a network stream, stdin remains open after this sequence is read, waiting for future data. The data can use either =
or ;
as a delimiter, and either delimiter could appear at any time. I’d want gawk to read until either delimiter is found, and then execute the body of my gawk script with whatever data was found, while ensuring that it properly handles the continuous stream of stdin. I explain this part in more detail below.
Here is what I have tried so far (zsh script that uses gawk). For this question, I simplified the body to just print the data – my full gawk script has a much more complicated body. I also simplified the netcat stream to instead cat
an example file, along with cat’ing stdin in order to mimic the stream behavior.
cat -u example.txt - | gawk 'BEGIN { RS = "=|;"; } { print $0; fflush(); }'
example.txt
AAAA=AAAA;AAAA;AAAA=
My attempt successfully handles most of the data……up until the most-recent record. It hangs waiting for more data from stdin, failing to execute the body of my script for the most-recent record, despite an appropriate delimiter clearly being available in stdin.
Current output:
AAAA
AAAA
AAAA
[hang here, waiting for future data]
Desired output: (difference being that all 4 records are successfully processed)
AAAA
AAAA
AAAA
AAAA
[hang here, waiting for future data]
After some debugging, I determined that if stdin is closed and a regex is used for RS, no problems occur. Conversely, if stdin remains open and RS is a plaintext string, no problems occur either. The problem only occurs if both stdin remains open and RS is a regex. Presumably it hangs because it’s waiting for more data in order to evaluate the regex….but a matching delimiter is clearly available on stdin, so I’d want gawk to handle that immediately. This seems to be somewhat of an edge-case scenario.
How can I accomplish this? Thank you all very much for your help!
user12280249 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.