sdf
1
Can anybody help on the script to combine/concatenate columns from multiple files
input1
4 135
5 185
6 85
11 30
16 72
17 30
21 52
22 76
input2
2 50
4 50
6 33
8 62
10 25
12 46
14 42
15 46
output
nr input1 input2
2 50
4 135 50
5 185
6 85 33
8 62
10 25
11 30
12 46
14 42
15 46
16 72
17 30
21 52
22 76
look into the join command
join -a 1 -a 2 file1 file2
Here is a try in perl (works for more than two input files, too):
Save code as comb.pl and call with: perl comb.pl input1 input2 ...
#!/usr/bin/perl
exit if $#ARGV < 0;
for ($i=0; $i<=$#ARGV; $i++) {
push @init, "";
}
$n=0;
push @header, "nr";
while ($ARG = shift) {
push @header, $ARG;
open(IN, $ARG) || die "cannot open $ARG";
while (<IN>) {
chomp;
split;
$entry{@_[0]} = [ @init ] if !defined($entry{@_[0]});
$entry{@_[0]}[$n] = @_[1];
}
$n++;
}
for ($i=0; $i<=$n; $i++) {
printf("%-10s", $header[$i]);
}
print "\n";
foreach $key (sort {$a <=> $b} keys(%entry)) {
printf("%-10s", $key);
for ($i=0; $i<$n; $i++) {
printf("%-10s",${entry{$key}}[$i]);
}
print "\n";
}