<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/css" href="/stylesheets/rss.css"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/">
  <channel>
    <title>Vidul Nikolaev Petrov: Tag splitter</title>
    <link>http://www.vidul.com/articles/tag/splitter</link>
    <language>en-us</language>
    <ttl>40</ttl>
    <description></description>
    <item>
      <title>File Splitter</title>
      <description>&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_perl "&gt;use strict;
use Getopt::Std;
use File::Basename;

getopts('scf:p:'); 

our($opt_s, $opt_c, $opt_f, $opt_p);

my $file = $opt_f;
my $size = $opt_p || 1;
my $len  = 1024;
my $c = 1;
my ($buf, $counter);

-f $file or usage();

if($opt_s){
    $size = 1024 * $size;

    open IN, &amp;quot;&amp;lt; $file&amp;quot; or die &amp;quot;cannot open_r $file $!&amp;quot;;
    open OUT, &amp;quot;&amp;gt; $file.$c&amp;quot;  or die &amp;quot;cannot open_w $file $!&amp;quot;;
    binmode IN;
    binmode OUT;

    while(read(IN, $buf, $len)){
        $counter++;
        if($counter &amp;gt; $size){
            $counter = 0;
            $c++;
            close OUT;
            open OUT, &amp;quot;&amp;gt; $file.$c&amp;quot;  or die &amp;quot;cannot open_w $file $!&amp;quot;;
            binmode OUT;
        } 
        print OUT $buf
    }
} elsif($opt_c) {
    my @files = grep { -f and /^$file\.\d+$/ } glob '*';
    my $newfile = &amp;quot;splitter_$file&amp;quot;;
    open OUT, &amp;quot;&amp;gt;&amp;gt; $newfile&amp;quot; or die &amp;quot;cannot open_w $file $!&amp;quot;;
    binmode OUT;
    map { $_ =~ s/^$file\.// } @files;
    for(sort {$a&amp;lt;=&amp;gt;$b} @files ){
        open IN, &amp;quot;$file.$_&amp;quot; or die &amp;quot;cannot open_r $file $!&amp;quot;;
        binmode IN;
        print OUT $_ while &amp;lt;IN&amp;gt;;
        close IN;
    }
} else {
    usage()
}

sub usage{
    my $pro = basename($0);
    print &amp;lt;&amp;lt;SQ;

$pro (-s|-c) -p piece size -f filename

    -s    split a file into pieces
    -c    collect a file from pieces
    -p     chunk size (defaults to 1MB)
    -f     file to be processed
SQ
    exit 1
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;</description>
      <pubDate>Fri, 18 Jan 2008 22:28:00 -0700</pubDate>
      <guid isPermaLink="false">urn:uuid:39d0f585-3124-44cf-a51a-eeb818f55b44</guid>
      <author>root</author>
      <link>http://www.vidul.com/articles/2008/01/18/file-splitter</link>
      <category>Perl</category>
      <category>scripts</category>
      <category>splitter</category>
    </item>
  </channel>
</rss>
