2つの列の値が別々のファイルの単一の列に発生した場合の行の抽出

2つの列の値が別々のファイルの単一の列に発生した場合の行の抽出

タブで区切られた2つのファイルがあります。

ファイル1

123   456
135   567
234   478

ファイル2

123    notimportant    notimportant2
456    notimportant    notimportant2
987    notimportant    notimportant2
135    notimportant    notimportant2
234    notimportant    notimportant2
478    notimportant    notimportant2

単一行の両方のエントリがfile2の最初の列にある場合は、file1から行を抽出する必要があります。したがって、出力ファイルは次のようになります。

出力

123   456
234   478

以前にfile1の最初の列がfile2の最初の列と一致した場合は、このawkコマンドを使用して行を抽出しました。

awk 'FNR==NR{a[$1];next}($1 in a){print}' file2 file1

ところで、どのように延長すべきか分からない。

ベストアンサー1

awk 'FNR==NR{a[$1]++;next;}($1 in a)&&($2 in a){print}' file2 file1

perl -lane '
   @ARGV and $h{$F[0]}++,next;
   print if @F == grep { $h{$_} } @F;
' file2 file1

おすすめ記事