我想在 File2 中查找 File1 第 1 列的模式,然后在 File2 旁边打印 File1 的第二列:
文件1(两列制表符分隔):
APBW lung
APCA non virulent
ABKM lung
APBX lung
KK020 -
APBZ non virulent
AOSU lung
APBY non virulent
APBV joint; lung; CNS
CP001321 virulent
APBT virulent
APBU non-virulent
APCB moderadamente virulenta (nose)
CP005384 -
文件2(两列制表符分隔):
HS372_00243 gi|219690483|gb|CP001321.1|
HS372_00436 gi|529264994|gb|APBX01000055.1|
HS372_00445 gi|529256455|gb|APBT01000061.1|
HS372_00544 gi|529259149|gb|APBV01000035.1|
HS372_00545 gi|529259149|gb|APBV01000035.1|
HS372_00546 gi|529259149|gb|APBV01000035.1|
所需输出(三列制表符分隔):
HS372_00243 gi|219690483|gb|CP001321.1| virulent
HS372_00436 gi|529264994|gb|APBX01000055.1| lung
HS372_00445 gi|529256455|gb|APBT01000061.1| virulent
HS372_00544 gi|529259149|gb|APBV01000035.1| jointlungCNS
HS372_00545 gi|529259149|gb|APBV01000035.1| jointlungCNS
HS372_00546 gi|529259149|gb|APBV01000035.1| jointlungCNS
临时 bash 代码(不起作用),但对其他语言开放:
while read vl; do grep "$vl" File2 ; done < File1
也尝试使用 awk(不起作用,因为它似乎正在寻找完全匹配,并且我在 File2 中的字符串被其他东西包围):
awk 'BEGIN { FS = OFS = "t" } FNR==NR{a[$1]=$0;next}($1 in a){print a[$1],$2,$3}' File1 File2
谢谢,贝尔纳多
像这样的东西听起来像是你想要的:
awk '
BEGIN { FS=OFS="t" }
NR==FNR { map[$1] = $2; next }
{
for (key in map)
if ($0 ~ key)
$0 = $0 OFS map[key]
print
}
' file1 file2
希望这对你有帮助
file2_contents = [i.strip() for i in open("file2.txt").readlines()]
with open("file1.txt") as file1:
for each_line in file1.readlines():
code=each_line.split('t')[0].strip()
part=each_line.split('t')[1].strip()
for each_file2_contents in file2_contents:
if code in each_file2_contents:
print each_file2_contents+'t'+part
在 Perl 中:
use warnings;
use strict;
use Data::Dumper;
my $str1 = <<"EOS1";
APBW lung
APCA non virulent
ABKM lung
APBX lung
KK020 -
APBZ non virulent
AOSU lung
APBY non virulent
APBV joint; lung; CNS
CP001321 virulent
APBT virulent
APBU non-virulent
APCB moderadamente virulenta (nose)
CP005384 -
EOS1
my $str2 = <<"EOS2";
HS372_00243 gi|219690483|gb|CP001321.1|
HS372_00436 gi|529264994|gb|APBX01000055.1|
HS372_00445 gi|529256455|gb|APBT01000061.1|
HS372_00544 gi|529259149|gb|APBV01000035.1|
HS372_00545 gi|529259149|gb|APBV01000035.1|
HS372_00546 gi|529259149|gb|APBV01000035.1|
EOS2
#HS372_00243 gi|219690483|gb|CP001321.1| virulent
my %data1;my %data2;
foreach my $line ( split(/n+/,$str1) ){
my @f = split(/t/,$line);
$data1{$f[0]} = $f[1];
}
my @data2 = split(/n+/,$str2);
foreach my $line ( split(/n+/,$str2) ){
my @f = split(/t/,$line);
my @sf = split(/|/,$f[1]);
$data2{$sf[3]} = $line;
}
#print Dumper(%data2);
foreach my $s1 ( sort { length($b) <=> length($a) } keys %data1){
foreach my $d2 (@data2){
my @f = split(/t/,$d2);
my @sf = split(/|/,$f[1]);
if ($sf[3] =~ m!^$s1!is){
#warn "found $s1 in $d2n";
print "$d2t$data1{$s1}n";
}
}
}
output:
HS372_00243 gi|219690483|gb|CP001321.1| virulent
HS372_00445 gi|529256455|gb|APBT01000061.1| virulent
HS372_00544 gi|529259149|gb|APBV01000035.1| joint; lung; CNS
HS372_00545 gi|529259149|gb|APBV01000035.1| joint; lung; CNS
HS372_00546 gi|529259149|gb|APBV01000035.1| joint; lung; CNS
HS372_00436 gi|529264994|gb|APBX01000055.1| lung