in reply to Adding a particular column from one file to a second file
#!/usr/bin/perl -w use strict; my %col2; #gene in column 1 to special column 2 my ($file1, $file2) = @ARGV; if (@ARGV != 2) { print "some usage here\n"; exit(); }; open (my $two_cols, '<', $file2) or die "unable to open $file2 $!"; open (my $big_file, '<', $file1) or die "unable to open $file1 $!"; %col2 = map{ s/\s+$//; #chomp + zap trailing white space my ($gene, $v) = split(/\t/,$_,3); }<$two_cols>; while (<$big_file>) { s/\s+$//; my ($gene, $rest) = split(/\t/,$_,2); if (my $lastcol = $col2{$gene}) { print "$gene\t$rest\t$lastcol\n"; } else { warn "$gene not found in $file2\n"; } } __END__ C:\TEMP>type f1.txt gene1 1234 78975 gene13 9876 ldlgfjk C:\TEMP>type f2.txt gene1 abc gene13 xyz C:\TEMP>update.pl f1.txt f2.txt gene1 1234 78975 abc gene13 9876 ldlgfjk xyz
|
|---|