Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I want netcat to pipe the output of a tcp/ip connection to commands that examine the output and split it into distinct files separated by the strings "msg" and "/msg" from the output, something like

nc host port | while :
do
split 'msg' '/msg' > timestamp.txt
done

but I can't get the syntax right. In an ideal world, I'd also like the whole of the output of nc appending to another file, say big_data.txt. Maybe I should use grep?

I know how to send nc output to a file and can work out how to split it, but the file will keep appending for 12 hours each day. Also, I think it's more efficient to split the data as it arrives.

share|improve this question
add comment

2 Answers 2

I would not do this with shell scripting1, and definitely not try to do it with a oneliner.

But if you want to pipe data into multiple files/pipes it could be done with the tee command.
Have a look at this SO-question for an example

1 I'd use perl or python

share|improve this answer
    
Why not? I can do this in R with strsplit, make.socket, read.socket and file commands. I was hoping to get it all processed as it arrives, assuming there's some buffer to get a faster solution. Maybe a shell script? –  Yoda May 17 '13 at 13:58
add comment

I did it all in R.

    stream_processor <- function(host_,port)
     con2 <- socketConnection(host=host_, port=port_)
    # as non-blocking, may need to loop for input
    readLines(con2)
    while(isIncomplete(con2)) {
        Sys.sleep(1)
        z <- unlist(readLines(con2,1000))
     write(z, file=paste(make.names(date()),'.stream',sep=''))
  }
}
share|improve this answer
add comment

Your Answer

 
discard

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

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