複数回表示される列識別子と一意の行を含む2つのテーブルのマージ

複数回表示される列識別子と一意の行を含む2つのテーブルのマージ

昨日も同様の質問をしました(複数回表示される列識別子を含む2つのテーブルのマージ)しかし、独自の行の問題に直面しました。

列1に基づいて2つのテーブルをマージしたいと思います。

ファイル1:

1 今日
1 緑
2 明日
3 赤

ファイル2:

1 多く
1 時々
2 仕事で
2 家で
2 時々
3 新しい
4 多く
5 時々
6 仕事で

期待される出力(ファイル3):

1 今日たくさん
1 今日時々
1 緑 時々たくさん
1 緑 時々
2 明日 出勤
2 明日 自宅で
2 明日 時々
3 赤鳥

私は次のことを思い出しました。

awk -F '[\t]' -v OFS='\t' '{i=$1;$1=x} NR==FNR{A[i]=$0;next} A[i]{print i,$0A[i]}' file2 file1 > file3

しかし、それは私にのみ提供します:

1今日の時々
2時々明日
3赤い鳥

前のスレッド(joinとawk)のソリューションは、2つのファイルの組み合わせ(すべての行を含む)を提供します。ファイル1の行(列1を識別子として)だけを含めたいのですが、ファイル2のすべての一致を報告します。

編集する:

タブで区切られた列

実際のファイル1:fig | 395961.4.peg.2627細菌南細菌不明

(1列:図... 2列:バクテリア... 3列1795)

実際のファイル2:fig | 1000561.3.peg.1838システイン脱硫酵素(EC 2.8.1.7)テスト - チアミン補助因子、ビタミン、補欠分子団、色素

(1列:図…2列:システイン…3列テスト…)

ベストアンサー1

私はPerlでこれをします:

#!/usr/bin/env perl 
use strict;

my (%file1,%file2);

## Open the 1st file
open(A,"file1");
while(<A>){
    ## Remove trailing newlines
    chomp; 
    ## Split the current line on tabs into the @F array.
    my @F=split(/\t/); 
    ## This is the tricky part. It adds fields 2-last
    ## to the hash $file1. The value of this hash is an array
    ## and the keys are the 1st fields. This will result in a list
    ## of all 1st fields and all their associated columns.
    push @{$file1{$F[0]}},@F[1..$#F];
} 


## Open the 2nd file
open(B,"file2");
while(<B>){
    ## Remove trailing newlines
    chomp; 
    ## Split the current line on tabs into the @F array.
    my @F=split(/\t/); 

    ## If the current 1st field was found in file1
    if (defined($file1{$F[0]})) {
        ## For each of the columns associated with
        ## this 1st field in the 1st file.
        foreach my $col (@{$file1{$F[0]}}) {
            print "$F[0]\t$col\t@F[1..$#F]\n";
        }
    }
} 

(長い)1行に入力できます。

$ perl -lane 'BEGIN{open(A,"file1"); while(<A>){chomp; @F=split(/\t/); 
                    push @{$k{$F[0]}},@F[1..$#F];}  } 
              $k{$F[0]} && print "$F[0]\t@{$k{$F[0]}}\t@F[1..$#F]"' file2
1   today green a lot
1   today green sometimes
2   tomorrow    at work
2   tomorrow    at home
2   tomorrow    sometimes
3   red new

大容量ファイルで作業している場合は、しばらく実行してみてください。

おすすめ記事