I have two .txt files. File1.txt and File2.txt. How can i merge these two files in unix (may be with awk) based on one common column.
File1.txt
looks like
Sub_ID Sam_ID v1
1878372 2253734 SAMN06396112
1883177 2264293 SAMN06414028
1884646 2275341 SAMN06432785
1860945 2277481 SAMN06407597
File2.txt
looks like
Sam_ID code V3 V4
2253734 20481 NA DNA
2275341 20483 NA DNA
2277481 20488 NA DNA
Final output file after merging should look like this
Finalfile.txt
Sub_ID Sam_ID v1 code V3 V4
1878372 2253734 SAMN06396112 20481 NA DNA
1884646 2275341 SAMN06432785 20483 NA DNA
1860945 2277481 SAMN06407597 20488 NA DNA
I have tried Join so far but may be i am not fully understanding the command (being new to unix).
sort -k2b File1.txt >sorted_file1.txt
sort File2.txt >sorted_file2.txt
join -1 2 sorted_file1.txt sorted_file2.txt > Finalfile.txt
I understand that by k2b i am nominating the second column of File_1 to be common among two and then merging.