-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaddFieldNames.pl
More file actions
executable file
·43 lines (31 loc) · 874 Bytes
/
addFieldNames.pl
File metadata and controls
executable file
·43 lines (31 loc) · 874 Bytes
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
#!/usr/bin/perl -w
if((@ARGV) < 2) {
print "\nUsage: ~ <input.csv> <output.csv> <fld1> ... <fldn> or\n";
print "\nUsage: ~ <input.csv> <output.csv>\n";
exit(1);
}
use Flat;
my($in) = Flat->new1($ARGV[0]);
if($in->hasHeader()) {
# die "Input file $ARGV[0] already has header: ", $in->getFieldNames(), "\n";
}
my($out) = $ARGV[1];
my($numFlds) = $in->getNumOfFields();
if(scalar(@ARGV) == $numFlds + 2) {
shift @ARGV;
shift @ARGV;
$in->setFieldNames(@ARGV);
}
elsif(scalar(@ARGV) == 2) {
my(@fldNames);
for(my($i) = 0; $i < $numFlds; $i++) {
$fldNames[$i] = "field$i";
}
$in->setFieldNames(@fldNames);
}
else {
Util::dieIt("Incorrect number of fields specfied: expecting ", $numFlds, ", but got ", scalar(@ARGV), "\n");
}
my($numOfRows) = $in->getNumOfRows();
$in->writeToFile("$out.tmp");
`mv $out.tmp $out`;