-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcatColumns.pl_save
More file actions
executable file
·74 lines (54 loc) · 1.54 KB
/
catColumns.pl_save
File metadata and controls
executable file
·74 lines (54 loc) · 1.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#!/usr/bin/perl -w
if((@ARGV) < 2) {
print "Usage: ~ [-o] <file1.csv> <file2.csv> ... <filen.csv> <out.csv>\n\n";
print "-o\trewrite the output file if exists\n";
print "Merge the data files line by line, assuming each line in a file corresponds to the same line in any other file\n\n";
exit(1);
}
use Flat;
use Getopt::Std;
my(%options);
getopts("o", \%options);
my $out = pop @ARGV;
if(!(exists $options{"o"})&&(-e $out)) {
die "$out exists already\n";
}
my(@fileNames) = @ARGV;
if(scalar(@fileNames) == 1) {
`cp $fileNames[0] $out`;
exit(0);
}
my(@files, @fieldNamesArrays);
for(my($i) = 0; $i < (@fileNames); $i++) {
$files[$i] = Flat->new1($fileNames[$i]);
if($files[$i]->hasHeader()) {
@{$fieldNamesArrays[$i]} = $files[$i]->getFieldNames();
}
else {
@{$fieldNamesArrays[$i]} = map { "FIELD$_"; } (1..$files[$i]->getNumOfFields());
}
}
open OUT, "+>$out" or die "Cannot open $out\n";
# print field names
print OUT $fieldNamesArrays[0][0];
for(my($j) = 1; $j < (@{$fieldNamesArrays[0]}); $j++) {
print OUT "\t$fieldNamesArrays[0][$j]";
}
for(my($i) = 1; $i < (@fileNames); $i++) {
for(my($j) = 0; $j < (@{$fieldNamesArrays[$i]}); $j++) {
print OUT "\t$fieldNamesArrays[$i][$j]";
}
}
print OUT "\n";
# print data lines
my @row;
while($row0 = $files[0]->readNextRow()) {
push @row, @{$row0};
for(my($j) = 1; $j < scalar(@files); $j++) {
$rowj = $files[$j]->readNextRow();
push @row, @{$rowj};
}
print OUT join("\t", @row), "\n";
@row = ();
}
close OUT;