join command in Linux with examples

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

"<yoastmark

Software Requirements and Linux Command Line Conventions
Category Requirements, Conventions or Software Version Used
System Any Linux distro
Software join
Other Privileged access to your Linux system as root or via the sudo command.
Conventions # – requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command
$ – 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

  1. 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 file01 and file02.
    $ join file01 file02
    

    "Running

    As you can see in the screenshot above, the output of the join command has combined each line from file01 and file02. So, now we have the list of foods from file01 corresponding 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.

  2. 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
    

    "Using

    As you can see in the screenshot above, the output from join merges file01 and file02 but 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.

  3. 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 -a option and a file number to save us some time.
    $ join -a 1 file01 file02
    
    Using join with the -a option to print the unmatched lines in the output
    Using join with the -a option to print the unmatched lines in the output

    Passing the -a option to the join command to merge file01 and file02 without 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 -a option if you do not include it.

    $ join -a [file number] file01 file02
    

    Using the -a option without specifying the file number outputs an error message
    Using the -a option without specifying the file number outputs an error message
  4. Specifying the file number for the join command, we can get it to print the unmatched line in file01 without 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
    

    "Using

    As you can see in the screenshot above, the join command, when used in conjunction with the -a option 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 file02.

NOTE
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.

Advanced Usage

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

  1. 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
    

    "Using

    With this option, as you can see in the screenshot above, the -1 and -2 options for the join command merged file01 and file02. Using these options, we were able to include the respective colors for each food, despite their not being present in both files.

  2. If the common field is the same in both file, we can use the -j option instead of the -1 and -2 options.
    $ join j1 file01 file02
    

    Using the join command's  -j option to merge file01 and file02 with the same matching field in both files
    Using the join command’s -j option to merge file01 and file02 with the same matching field in both files

Closing Thoughts

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.