-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcatColumns.pl
More file actions
executable file
·64 lines (46 loc) · 1.4 KB
/
catColumns.pl
File metadata and controls
executable file
·64 lines (46 loc) · 1.4 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
#!/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, @fieldNames);
for(my($i) = 0; $i < (@fileNames); $i++) {
$files[$i] = Flat->new1($fileNames[$i]);
my @flds;
if($files[$i]->hasHeader()) {
@flds = $files[$i]->getFieldNames();
}
else {
@flds = map { "FIELD$_"; } (1..$files[$i]->getNumOfFields());
}
push @fieldNames, @flds;
}
open OUT, "+>$out" or die "Cannot open $out\n";
# print field names
# print OUT join("\t", map { $orig = $_; $orig =~ s/\s//g; $orig} @fieldNames), "\n";
print OUT join("\t", @fieldNames), "\n";
# print data lines
while($row = $files[0]->readNextRow()) {
my @rowData = @{$row};
for(my($i) = 1; $i < scalar(@files); $i++) {
push @rowData, @{$files[$i]->readNextRow()};
}
# print OUT join("\t", map { $orig = $_; $orig =~ s/\s//g; $orig} @rowData), "\n";
print OUT join("\t", @rowData), "\n";
}
close OUT;