1
|
1 #!/usr/bin/perl
|
|
2 # Splits SD records into multiple files of equal no. of records
|
|
3 use lib "$ENV{'RBT_ROOT'}/lib";
|
|
4
|
|
5 use FileHandle;
|
|
6 use SDRecord;
|
|
7
|
|
8 # Record size to split into
|
|
9 my $recSize = 1000;
|
|
10
|
|
11 #output root
|
|
12 my $outRoot = "tmp";
|
|
13
|
|
14 #Print help if no command line arguments
|
|
15 printHelpAndExit() if (scalar(@ARGV) == 0);
|
|
16
|
|
17 #Parse command line arguments
|
|
18 my @files;
|
|
19 while (scalar(@ARGV)) {
|
|
20 my $arg = shift @ARGV;
|
|
21 printHelpAndExit() if ($arg eq '-h');
|
|
22 if (index($arg,'-o')==0) {
|
|
23 $outRoot = substr($arg,2);
|
|
24 }
|
|
25 elsif (index($arg,'-')==0) {
|
|
26 $recSize = substr($arg,1);
|
|
27 }
|
|
28 else {
|
|
29 push @files,$arg;#must be a filename
|
|
30 }
|
|
31 }
|
|
32 push @ARGV,@files;#put the filenames back in the arg list
|
|
33
|
|
34 my $sdRec = new SDRecord;
|
|
35 my $nRec=0;
|
|
36 my $nFile=0;
|
|
37 my $sdfh;
|
|
38
|
|
39 #read records
|
|
40 while ($sdRec->readRec('LINES'=>1)) {
|
|
41 #check if we need to start a new output file
|
|
42 if ($nRec % $recSize == 0) {
|
|
43 $nFile++;
|
|
44 my $outFile = $outRoot . $nFile . ".sd";
|
|
45 if (defined $sdfh) {
|
|
46 undef $sdfh;
|
|
47 }
|
|
48 $sdfh = new FileHandle ">$outFile";
|
|
49 if (!defined $sdfh) {
|
|
50 die "Can't open $outFile";
|
|
51 }
|
|
52 else {
|
|
53 print STDOUT "Opening $outFile\n";
|
|
54 }
|
|
55 $sdfh->autoflush(1);
|
|
56 select($sdfh);
|
|
57 }
|
|
58 $nRec++;
|
|
59 $sdRec->writeRec();
|
|
60 }
|
|
61 undef $sdfh;
|
|
62 select(STDOUT);#reselect STDOUT as default
|
|
63
|
|
64 #######################################################################
|
|
65 sub printHelpAndExit {
|
|
66 print "\Splits SD records into multiple files of equal size\n";
|
|
67 print "\nUsage:\tsdsplit [-<RecSize>] [-o<OutputRoot>] [sdFiles]\n\n";
|
|
68 print "\t-<RecSize>\trecord size to split into (default = 1000 records)\n";
|
|
69 print "\t-o<OutputRoot>\tRoot name for output files (default = tmp)\n";
|
|
70 print "\n\tIf SD file list not given, reads from standard input\n";
|
|
71 exit;
|
|
72 }
|