Tell me more ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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 | handleMessage

This code works fine when log messages are delimited by new lines like the following

Service started

ERROR: Something terrible happened

Service stopped

But 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 happened

NullPointerException at SomeMethod (File1.java)

SomeOtherMethod (File2.java)LOG0003 Service stopped

Using 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?

share|improve this question
2  
I think your best bet is to pipe nc's output to sed to do the line-splitting, and give the result of that mangling to your script. – vonbrand Feb 1 at 19:48

Know someone who can answer? Share a link to this question via email, Google+, Twitter, or Facebook.

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.