I'm working on a set of scripts to monitor some external services that log to a TCP port.function handleMessage {  while read message  do    # Handle $message  done}nc -d $ipadd $port | handleMessageThis code works fine when log messages are delimited by new lines like the followingService startedERROR: Something terrible happenedService stoppedBut one of the services delimits messages using LOGXXXX and allows new lines within messages so it's output looks more like this:LOG0001 Service startedLOG0002 ERROR: Something terrible happenedNullPointerException at SomeMethod (File1.java)SomeOtherMethod (File2.java)LOG0003 Service stoppedUsing my code above handleMessage is called for each of the lines instead of for each set of LOGXXXX segments and if time passes with no messages containing newlines my script gets very far behind in handling messages.Is there an option on nc or another program I can use to read from this TCP port and break on a custom delimiter, or some option to have a bash function handle each write to stdout instead of only being called for each line?
Log from TCP Port breaking messages on characters other than newline
bash;logs;netcat
The problem here is in your while read message line. read by default terminates on a newline, but you can pass it another character with the -d option.The question to ask is in your LOGXXXX lines, how does the script know when the entry is finished? If you can put a special character at the end then you can use read -d. Otherwise, you could replace the newlines with something else and then put them back after.So in your example:function handleMessage {  while read message  realmessage=`echo $message | tr '|' '\n'`  do    # Handle $realmessage  done}nc -d $ipadd $port | tr '\n' '|' | handleMessageSo you're replacing all the newlines with a | so that it'll all get in the read call, and then you split it out after.