by Oliver; 2014
58. join
join joins two sorted files on a common key (the first column by default).
If:
$ cat tmp1.txt
1 a
2 b
3 c
$ cat tmp2.txt
2 aa
3 bb
Then:
$ join tmp1.txt tmp2.txt
2 b aa
3 c bb
My lab uses a short Perl script called
tableconcatlines (written by my co-worker, Vladimir) to do a join without requiring the files to be pre-sorted:
#!/usr/bin/env perl
# About:
# join two text files using the first column of the first file as the "key"
# Useage:
# tableconcatlines example/fileA.txt example/fileB.txt
%h = map {/(\S*)\s(.*)/; $1 => $2} split(/\n/, `cat $ARGV[1]`);
open $ifile, '<', $ARGV[0];
while (<$ifile>)
{
/^(\S*)/;
chop;
print $_ . "\t" . $h{$1} . "\n";
}
<PREV
NEXT>