LINUX.IE, website of the Irish Linux Users' Group
Tux rules!

   
Home
New Users
Articles
Download
Projects
Community
Vendors

  Print Version
Email to...
 
Archives:


planetILUG

Recent News

News Archive


Join the
ILUG
on FaceBook


Join the
ILUG
on LinkedIn


Join the
ILUG SETI
Group



















 
 :: Mailing Lists

[ILUG] Breaking up an input stream

[ILUG] Breaking up an input stream

Niall O Broin niall at linux.ie
Fri Jan 11 01:00:32 GMT 2002


On Thu, Jan 10, 2002 at 12:42:59PM -0000, Adrian Flynn meant to write:

> There is a continuous data stream coming in the serial port.
> There is a FF character at the end of each record.
> Ideally, each record should be stored in a separate file for further
> processing (these records are rather large)
> The question is how to recognise the End-Of-Record character and store all
> data from that record in a (uniquely named) file.
> There is unique data within each record which could form the basis for the
> filename.
>
> Any ideas (sed/awk etc)

Seems relatively trivial but I don't think sed or awk are the tools for the
job - but I'm sure somebody will come up with awk for it - I'd be well
impressed if anybody can do this in sed. Perl would be my poison and something
like this might be a good start

#!/usr/bin/perl
while (<>) { # while there's a character to read from STDIN
    if (/\f/) { # check if it's an FF character and if so, file the data
       $filename = substr($buffer, 1, 10); # figure out the filename however
       open FILE, ">$filename"; # open a file for writing with that name
       print FILE $buffer;      # write the buffer to the file
       close FILE;
       $buffer = "";            # clear the buffer
    } else {    # not an FF so we must put it in the buffer
       $buffer .= $_;
    }
}


The above is absolutely not guaranteed to do anything for you whatsoever
apart from compile. Running this with the serial port as STDIN should do
about what you want but it's COMPLETELY without any kind of error checking or
sanity checking and relies on the OS to do all the buffering / handshaking
on the serial port for you (which is what an OS is for, after all, but you
may have more stringent requirements)

If this is intended for production use and if you go with Perl and you end
up with a program which is not at least twice as long as the above then
you've certainly not got enough error checking :-(


Niall




More information about the ILUG mailing list
Read this without the formatting.
                                                                                                    

 

Hosted by HEAnet


Maintained by the ILUG website team. The aim of Linux.ie is to support and help commercial and private users of Linux in Ireland. You can display ILUG news in your own webpages, read backend information to find out how. Networking services kindly provided by HEAnet, server kindly donated by Dell. Linux is a trademark of Linus Torvalds, used with permission. No penguins were harmed in the production or maintenance of this highly praised website. Looking for the Indian Linux Users' Group? Try here. If you've read all this and aren't a lawyer: you should be!
RSS Version
Powered by Dell