dataset 1: vr 1 2 3 5 o1 a a b b o2 c c d d o3 e e f f dataset 2: id date1 age 1 2005 30 2 2006 25 3 2005 22 4 2004 23 5 2006 25 merged/tranposed dataset: id date1 age o1 o2 o3 1 2005 30 a c e 2 2006 25 a c e 3 2004 22 b d f 4 2004 23 5 2006 25 b d f #### $ids = `head -1 dataset1`; @matchid = split(" ", $ids); shift(@matchid); open(IN, "dataset2" ) || die open(OUT, ">mergeddataset") || die ; $line=; chomp $line; $h = `cut -f1 dataset1`; @header = split(/\n/,$h); shift(@header); print OUT $line." ".join(" ",@header)."\n"; while ($line=) { chomp $line; @match=split(" ",$line); foreach $id (@header) { if ($id eq $match[1]) { $column=`head -1 dataset1|tr -s "\t" "\n" | grep -n $id|cut -f1 -d":"`; chomp $column; $a = `cut -f$column dataset1| tail +2 | tr -s '\n' ' '`; chomp $a; @b = split(/ +/, $a); print OUT $line." ".join(" ",@b)."\n"; } } } close(OUT); close(IN);