Sometimes you need to combine two text files basing on values in specified columns of that files (on the similar way like SQL JOIN operation). In the Linux it can be easily done using join shell command. Let’s take a look on the following example. Suppose we have two files:
$ cat ./file1.txt 1 aa 2 bb 3 cc 4 dd
$ cat ./file2.txt 1 ee 2 ff 3 gg 4 hh
Next we are executing following join command:
$ join -1 1 -2 1 ./file1.txt ./file2.txt 1 aa ee 2 bb ff 3 cc gg 4 dd hh
The above example joins file1.txt with file2.txt basing on the first column of the file1.txt (option -1 1) and the first column of the file2.txt (option -2 1).