Here's a demonstration of the join operation performed using two tools: join, which assumes a common ordering of the keys (i.e., sorted files), and AWK, which does not.
$ cat file1.txt gene1 1234 78975 gene13 9876 ldlgfjk $ cat file2.txt gene1 abc gene13 xyz $ join -t ' ' file1.txt file2.txt gene1 1234 78975 abc gene13 9876 ldlgfjk xyz $ awk 'FNR == NR { a[$1] = $2; next } { print $0, a[$1] }' \ > FS='\t' OFS='\t' file2.txt file1.txt gene1 1234 78975 abc gene13 9876 ldlgfjk xyz $
And here's a ham-handed translation of the AWK idiom into Perl.
$ perl -F'\t' -lane ' > $a{$F[0]} = $F[1], next if $#ARGV == 0; > print "$_\t$a{$F[0]}"; > ' file2.txt file1.txt gene1 1234 78975 abc gene13 9876 ldlgfjk xyz $
I use the AWK idiom a lot. It ain't Perl, but it's darned handy.
Jim
In reply to Re: Adding a particular column from one file to a second file
by Jim
in thread Adding a particular column from one file to a second file
by ZWcarp
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |