awk – 比较两个文件中的文件和打印行

我正在寻找一种比较两个文件的第2列和第1列的方法,如果它们与第一个文件中的所有列相同,而第二个文件中的列为$ 2。

file_1.txt

apple tree 5 great see 10 see apple 3 tree bee 11 make change 2 

file_2.txt

 apple 5.21 around 6.21 great 2 bee 1 see 7.43 tree 3 

输出应如下所示:

 apple tree 5 3 great see 10 7.43 see apple 3 5.21 tree bee 11 1 

我试过了

 awk 'NR==FNR{a[$2];next} ($1 in a) {print}' file_1.txt file_2.txt > output.txt 

这显然只是打印file_2.txt的匹配行。 那么如何为第一个文件的列添加print语句?

我试着读取数组中的更多列

 awk 'NR==FNR{a[$2];b[$1];c[$3];next} ($1 in a) {print a, bc}' file_1.txt file_2.txt > output.txt 

这显然是错的;)

非常感谢帮助。

怎么样

 $ awk 'NR==FNR {a[$1]=$2; next} $2 in a {print $0, a[$2]}' OFS='\t' file_2.txt file_1.txt apple tree 5 3 great see 10 7.43 see apple 3 5.21 tree bee 11 1