[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [Sheflug] Extracing data from a file
> After each of these records, a new one is found. I want to split
> this single file into a number of files. Each new file will have the
> name id_string, and the content of the file will be exactly the same
> record that is present in the initial file. The number of a,b data
> points is specified by N (there are N a and N b points).
Something like ...
###################################################################
#!/usr/bin/perl
use strict;
my ($file, $thing, $count, $i, $pair);
while ($file = <>)
{
open (FILE, ">$file") or die "Cannot open $file";
$thing = <>;
print FILE $thing;
$count = <>;
print FILE $count;
for ($i = 0; $i < $count; $i++) {
$pair = <>;
print FILE $pair;
}
close FILE;
}
####################################################################
but I don't quite grok the meaning of the last line of the record:
a_{N/2},b[N/2}
so adding that to the script is left as an exercise for the reader :-)
This could probably be improved ten-fold but my Perl's a little rusty, so
you'll have to take this :)
cut & paste into a script (say, splitit), chmod, then invoke as:
/path/to/script/splitit < /path/to/input/file
Files will be created in current directory. The script also has no error
checking, so if there is one corrupt record (eg, where N != number of
records), it'll get lost. You might be able to make use of the last line to
act as an end-of-record marker so the script can keep its sanity.
Chris...
--
\ Chris Johnson \ NP: Delerium - 02. Inside the Chamber
\ cej [at] nightwolf.org.uk ~-----,
\ http://cej.nightwolf.org.uk/ ~-----------------------------------,
\ Redclaw chat - http://redclaw.org.uk - telnet redclaw.org.uk 2000 \____
___________________________________________________________________
Sheffield Linux User's Group -
http://www.sheflug.co.uk/mailfaq.html
GNU the choice of a complete generation.