Welcome to forums, special thanks for using CODE TAGS for samples.
On UNIX.com we encourage users to show their efforts in their questions which they have put in order to solve their own problems, so kindly do add the same and let us know then.
I was using transposes the rows into columns, then data.frame creates a list of the columns to match the data match(data.frame(t(x)), data.frame(t(y)))
awk '
NR == FNR { if (!T[$2]) T[$2] = $1; next }
{ lin = 0; for (i=1; i<=NF; i++)
if ($i in T) lin++
}
lin == NF { for (i=1; i<=NF; i++) print T[$1] FS $i }
' Table2 Table1
otherwise
awk '
NR == FNR { if (!T[$2]) T[$2] = $1; next }
{ lin = 0; for (i=1; i<=NF; i++)
if ($i in T) lin++
}
lin == NF { for (i=1; i<=NF; i++) D[$i] = T[$i] }
END { for (i in D) print D FS i }
' Table2 Table1
maybe this is very redundant for such task,
but maybe this will anticipate possible problems with more complex input
This instance with integrated version control requires gnu awk
awk '
NR == FNR { T[$2] = $1 (T[$2]?FS T[$2]:""); next }
{ lin = 0; for (i=1; i<=NF; i++)
if ($i in T) lin++
}
lin == NF { for (i=1; i<=NF; i++) {
if (T[$i]) print $i FS gensub(/^.*\s/, "", 1, T[$i])
sub(/\s?[^ ]+$/, "", T[$i])
}
}
' Table2 Table1