As the name implies, we use the join command in Linux to combine data from text files with a common field. This command can be helpful for a variety of system administration situations.
For example, if you’re archiving personal information spread out across multiple files, you can use the join command to merge them. The join command can get quite tricky at times because of the restrictions it comes with. But in this tutorial, we’ll help you get around them and make the most of the join command.
In this tutorial, you’ll learn how to use the join command in Linux through examples. Follow along below to learn about the various options that you can use with this command to avoid those oh-so frustrating command execution errors.
In this tutorial you will learn:
- How to use the join command on Linux
|Category||Requirements, Conventions or Software Version Used|
|System||Any Linux distro|
|Other||Privileged access to your Linux system as root or via the
|Conventions||# – requires given linux commands to be executed with root privileges either directly as a root user or by use of
$ – requires given linux commands to be executed as a regular non-privileged user
Frequently Used Options
The join command will take two or more related text documents and “join” them in one or more common fields, taking seemingly fragmented data and combining it into something much more meaningful. Follow along with some of our examples below to see how it works.
join command in Linux Basic Examples
- Running the join command by itself, without any additional options, will merge the specified text files in the command line with a matching join field. The field that the join command uses by default is the first line of each text file. In our example below, we’ll use two text files containing foods and their food type. We’ll call these
$ join file01 file02
As you can see in the screenshot above, the output of the join command has combined each line from
file02. So, now we have the list of foods from
file01corresponding to the food types in
file02. But sometimes things don’t always go as smoothly, and we’ll have an additional line containing, for example, an extra food that doesn’t possess its food type correspondent from the merged text file. Don’t worry, though. Because in Linux, there’s always a way to soften the blow. We’ll cover that in the next example.
- If one of the text files you are merging with another is missing a line to be combined using the join command without additional options, the output will print an error stating that the file is not sorted. Therefore the join will not process that line in both files. If that’s a little confusing to visualize, it’ll be more clear from the example screenshot below.
$ join file01 file02
As you can see in the screenshot above, the output from join merges
file02but only prints the 3 lines that can be matched. This is because, as stated in the error message that proceeds the output, the text file with the missing line is not sorted.
With larger text files, it can be a lot more challenging to discern what lines are the cause for this error message. This can lead to many unnecessary headaches and wastes of time trying to figure out. We’ll show you how to prevent this in the following example.
- Now that you know what happens when you use the join command to combine files containing lines that this command is unable to match, we can start learning how to use the
-aoption and a file number to save us some time.
$ join -a 1 file01 file02
-aoption to the join command to merge
file02without specifying the file number that the error message in the previous example told us was not sorted will give you an error message. This error occurs because the join cannot know which file you are referring to with the
-aoption if you do not include it.
$ join -a [file number] file01 file02
- Specifying the file number for the join command, we can get it to print the unmatched line in
file01without an error message. As mentioned earlier, this saves a lot of time, as we don’t have to go looking for the non-pairable line(s) ourselves.
$ join -a 1 file01 file02
As you can see in the screenshot above, the join command, when used in conjunction with the
-aoption and the correct file number, the output prints the line containing our “pineapple” anyway. The rest of the line is blank because the food type of our “pineapple” is left a mystery as it is not present in
You can always use the man command to read more about the join command and its official documentation. Click the previous link to see how to open the manual pages for any command on a Linux system.
The join command is not terribly complex, but as you’ve observed throughout the examples section of this article, there exist a few restrictions that might cause some hangups. But we can utilize the convenient command line options available for the join command. Check out some of the command’s more advanced options below.
join command in Linux Advanced Examples
- As mentioned, the join command joins lines of text files together on a matching field. This field is the first line of each file by default. However, in the example below, we will show you how to customize which lines are used as the common field for join command.
$ join join -2 1 -1 2 file01 file02
With this option, as you can see in the screenshot above, the
-2options for the join command merged
file02. Using these options, we were able to include the respective colors for each food, despite their not being present in both files.
- If the common field is the same in both file, we can use the
-joption instead of the
$ join j1 file01 file02
In this tutorial, we learned all about the join command on Linux which is beneficial to master for general users and system administrators that frequently keep data in multiple text files.
It’s important to note that there are many different ways to achieve the same results in Linux. The join command is no exception to this. There are multiple ways to merge and sort text files using commands in Linux; the join command is just one of them.