I am looking for a way to compare column $2 and column $1 of two files and if they are the same than print all columns from the first file with column $2 from the second file.
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
The output should look like:
apple tree 5 3
great see 10 7.43
see apple 3 5.21
tree bee 11 1
I tried
awk 'NR==FNR{a[$2];next} ($1 in a) {print}' file_1.txt file_2.txt > output.txt
which obviously just prints the matching lines of file_2.txt. So how do I add the print statement for the columns of the 1st file?
I tried to read in more columns in an array like
awk 'NR==FNR{a[$2];b[$1];c[$3];next} ($1 in a) {print a, b c}' file_1.txt file_2.txt > output.txt
which is obviously wrong ;)
Help is very much appreciated.