[NTLUG:Discuss] Output to Multiple Files - Now More

Kevin Brannen kbrannen at pwhome.com
Thu Apr 7 21:59:14 CDT 2005


David Wilson wrote:

>Patrick,  you hit the nail on the head. Now where do I
>find someone to write that Perl or C program?
>
>  
>

It sounds really easy if I understand what you want, something like:

---
#!/usr/bin/perl

$input = "/dev/sdb"; # where the data comes from
$num_copies = 3; # or how many ever you need
$out_dir = "/data/copies"; # where you want the copies
$chunk_num = 0;
$chunk = "";
$chunk_size = 2**16; # or however big you need it

# make the output dirs
$cmd = "mkdir";
foreach $i (1 .. $num_copies) { $cmd .= " $out_dir/$i"; }
system($cmd);

# open input
open(IN, "<$input");

# read data 1 chunk at a time, write to each dir
while (read(IN, $chunk, $chunk_size))
{
    foreach $i (1 .. $num_copies)
    {
       open(OUT, ">$out_dir/$i/$chunk_num");
       print OUT $chunk;
       close(OUT);
    }
    $chunk_num++;
}
close(IN);
---

NOTE!!!  This is totally untested, looks like it should work, but you 
really should test this before you do anything.  Also, for the sake of 
brevity, I left out *ALL* error checking; you should not do that.  Error 
checking is left as an exercise for the reader. ;-)  Changing the top 
section to be command-line arg driven is pretty easy too, and would be 
done in a normal production script.

If you want this to be production quality, tested, etc.; email me 
offline and we can discuss it and lunch. :-)

HTH,
Kevin




More information about the Discuss mailing list