-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaddColumn.pl
More file actions
executable file
·50 lines (37 loc) · 1007 Bytes
/
addColumn.pl
File metadata and controls
executable file
·50 lines (37 loc) · 1007 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
44
45
46
47
48
49
50
#!/usr/bin/perl -w
use Util;
use Flat;
use Getopt::Std;
my(%options);
getopts("n", \%options);
if(scalar(@ARGV) != 4 && scalar(@ARGV) != 5) {
print "\nUsage: ~ [-n] <input_file> <field_index> <field_name> <field_val> [<out_file>]\n\n";
print " -n no field name is used. The specified field name is ignored.\n\n";
exit(1);
}
my $cmdLine = Util::getCmdLine();
my($in) = Flat->new1(shift @ARGV);
my $fldIndex = shift @ARGV;
my $fldName = shift @ARGV;
my $cval = shift @ARGV;
my $out;
if(scalar(@ARGV) == 1) {
$out = shift @ARGV;
}
else {
$out = $in->getFileName();
}
my $otmp = "$out.addColumn.tmp";
open OUT, "+>$otmp" or die "Cannot open $otmp\n";
if(!(exists $options{"n"})) {
my @fnames = $in->getFieldNames();
splice @fnames, $fldIndex, 0, $fldName;
print OUT join("\t", @fnames), "\n";
}
while($row = $in->readNextRow()) {
@rdata = @{$row};
splice @rdata, $fldIndex, 0, $cval;
print OUT join("\t", @rdata), "\n";
}
close OUT;
`mv $otmp $out`;