How to combined to file with their values

I have two files and I need to combine their values

example:

i have file1 and file2

in file1

1019 40 50
1119 55 62

in file2

1019 33 10
1119 12 44

desired output should be:
file3
1019:40:33:50:10
1119:55:12:62:44

any advice would be appreciated.. thanks :slight_smile:

Hi,

try:

join -o 1.1 1.2 2.2 1.3 2.3 file1 file2 | tr ' ' ':' >> file3

First join the two files according to the specified fields,
then replace all spaces by collons.

HTH Chris

Hi, below code can hlep you much i think.

it can combine any number of files with column of each file one by one as long as those files have same filed seperator.

#! /usr/bin/perl -w
sub combine{
	my $sep=shift;
	my $num=$#_+1;
	for ($i=0;$i<=$num-1;$i++){
		open FH,$_[$i] or die "Can not open file";
		while(<FH>){
			my @tmp=split($sep,$_);
			map {tr/\n//d} @tmp;
			for($j=0;$j<$#tmp;$j++){
				@{$hash{$tmp[0]}}->[$j*$num+$i]=$tmp[1+$j];
			}
		}
	}
	return \%hash;	
}
$ref=combine(" ","a.txt","b.txt","c.txt");
%hash=%{$ref};
for $key (keys %hash){
	print $key,":",join ":",@{$hash{$key}};
	print "\n";
}

i tried this one. but its not working..

any other advice?... that would be appreciated.

What exactly is not working?
What is the error message? What is the output?

It tried it on my debian box under bash and zsh and it is working fine.
You should find the output in a file called "file3".

nope its not that error... i just cant see the result of file3...

i can't combined values in my 2 file T_T

any help?.. :slight_smile: