Result

The solution to the previous problem is as simple as running the following statement (the trick here is knowing how to write a tab for the field separator, which can be performed through the combination ctr+v + <tab>. By the way, be careful if you copy the code from this page as the tab may be turned into a set of spaces!):

join -t’ ‘ File1 File2

Pretty simple, right? Now, lets do the same exercise but with a second file that contains the common column in a different order, such as the following:

In this particular situation, we will have to “sort the file using the last field and join them using the second file’s second field”. So, lets try to translate the previous English sentence into some shell commands, as in:

Lets try to dismistify the previous commands. The first awk statement will generate the contents of the original file but with the last field, followed by a ‘|’ in the beginning of the sentence, as in:

2|Career Manager 2
...

Then, the sort using ‘|’ as separator, sorts the lines by their ID number instead of their description. Next, we filter its result with awk again to remove the temporary part, i.e., the ID, followed by the ‘|’, and write the output to a new temporary file named File4_sorted. At this moment, we have exactly the same contents of the File3 and, in this case, we can issue exactly the same statement that we did for joining files 1 and 3. In the end, and just to clean things up, we remove the temporary file File4_sorted.

Explanations

Although join is a simple command, it is still useful and important. After all, it is one of the commands one must know for the LPIC 1 certification. Therefore, I hope this simple exercise can help you to better understand how to use it (it was useful for me, at least, as I could exercise what I have learned while studying for the certification)