LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
LinkBack Search this Thread
Old 11-06-2008, 07:42 AM   #1
d1s4st3r
Member
 
Registered: May 2004
Location: Italy
Distribution: Ubuntu, Slackware, OpenBSD, PC-BSD
Posts: 68

Rep: Reputation: 15
Bash, netcat, redirection and data extraction from stream


Hi folks,

I have the following problem concerning Bash scripting.

I'm using netcat called within a script to receive some data, plain text mixed with binary, and print it in on the screen in the following format:

Code:
[...]
------------------123456
DATADATADATADATADATADATADATADATADATA
DATADATADATADATADATADATADATADATADATA
DATADATADATADATADATADATADATADATADATA
DATADATADATADATADATADATADATADATADATA
DATADATADATADATADATADATADATADATADATA
DATADATADATADATADATADATADATADATADATA
DATADATADATADATADATADATADATADATADATA
DATADATADATADATADATADATADATADATADATA
DATADATADATADATADATADATADATADATADATA
------------------123456
[...]
Well, what I really need now is:
1) to find, inside of the data stream coming from netcat, the first occurence of the string "------------------123456" (we could define it as a "start boundary")
2) to start writing all the data coming next (DATADATADATA...DATADATADATA) to a file (note that such data could be both plain text and binary)
3) to stop writing data to that file once a second occurence of "------------------123456" (call it "end boundary") is found

My problem is that I need to do these things "on the fly", especially because that information is available only once and because the size of the incoming data is unknown, and I only have those two boundaries to know when start and stop writing data to a file.

Any kind suggestion to reach the goal would be greatly appreciated.

Thanks in advance.
 
Old 11-06-2008, 08:39 AM   #2
burschik
Member
 
Registered: Jul 2008
Posts: 159

Rep: Reputation: 31
Code:
sed -n '/pattern1/,/pattern2/ p'
 
Old 11-06-2008, 08:51 AM   #3
d1s4st3r
Member
 
Registered: May 2004
Location: Italy
Distribution: Ubuntu, Slackware, OpenBSD, PC-BSD
Posts: 68

Original Poster
Rep: Reputation: 15
Thank you, I'll try that as soon as I get home from work.

But - this is very important - I forgot to specify that the boundary may vary too. It simply respects a syntax like "-------------RANDOM_NUMBER".
This because there could me more than one block of data in the same stream, like this:

Code:
[...]
------------------123456
DATADATADATADATADATADATADATADATADATA
DATADATADATADATADATADATADATADATADATA
DATADATADATADATADATADATADATADATADATA
DATADATADATADATADATADATADATADATADATA
DATADATADATADATADATADATADATADATADATA
DATADATADATADATADATADATADATADATADATA
DATADATADATADATADATADATADATADATADATA
DATADATADATADATADATADATADATADATADATA
DATADATADATADATADATADATADATADATADATA
------------------123456
[...]
------------------789101
FOOFOOFOOFOOFOOFOOFOOFOOFOOFOOFOOFOO
FOOFOOFOOFOOFOOFOOFOOFOOFOOFOOFOOFOO
FOOFOOFOOFOOFOOFOOFOOFOOFOOFOOFOOFOO
FOOFOOFOOFOOFOOFOOFOOFOOFOOFOOFOOFOO
------------------789101
[...]
And DATADATA... should be written for example in /tmp/data, while FOOFOO... should be written in /tmp/foo.

That's all for now, I suppose.

Thanks again.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
TV stream with netcat on network RicardoB Linux - Networking 1 07-03-2007 12:22 AM
Generating UDP data gram using netcat mohtasham1983 Linux - Networking 3 02-19-2007 03:47 AM
Data extraction from a really, really huge file. thekillerbean Linux - Software 4 04-09-2006 04:18 AM
Stream redirection in Linus GCC sasha_baranov Programming 0 10-25-2004 08:18 AM
pcap data extraction black_man Linux - Networking 1 08-28-2004 08:52 AM


All times are GMT -5. The time now is 12:36 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration