How to Use the join Command to Merge Files by Common Fields in Linux

The Database Dilemma

If you are a systems administrator, you frequently encounter raw data stored in simple text files. Imagine you have two separate files. employee_ids.txt contains a list of ID numbers and employee names (e.g., “101 John Doe”). The second file, employee_salaries.txt, contains ID numbers and salaries (e.g., “101 $75,000”).

Your boss demands a single, unified report showing the employee’s name next to their salary. If you open both files in a graphical text editor and attempt to copy and paste the salaries manually, it will take hours, and you will inevitably make a catastrophic mistake.

You could write a complex Python script or import the data into a massive MySQL database to stitch the information together, but that is massive overkill. If the two files share a common mathematical anchor (in this case, the Employee ID number), you can merge them instantly, directly from the terminal, using the brilliant join command.

Step 1: The Golden Rule of Joining

Before you run the join command, you must obey its one, strict mathematical rule: both files must be perfectly sorted alphabetically or numerically based on the common field.

If employee_ids.txt starts with ID 105, and employee_salaries.txt starts with ID 101, the join command will instantly crash and output an error. It cannot search randomly; it reads both files line-by-line simultaneously.

Before attempting a join, always use the sort command to organize your files:

sort employee_ids.txt > sorted_ids.txt
sort employee_salaries.txt > sorted_salaries.txt

Step 2: Performing a Basic Join

Once both files are perfectly sorted, the syntax is incredibly simple. By default, the join command assumes that the very first column of text (Field 1) is the common anchor linking the two files together.

join sorted_ids.txt sorted_salaries.txt

The terminal will instantly output a beautifully merged list directly onto your screen. It finds “101” in the first file, finds “101” in the second file, and stitches the remaining data together on a single line: “101 John Doe $75,000”.

Step 3: Joining on Different Columns

In the real world, your data is rarely perfectly aligned. What if the Employee ID is the first column in the ids.txt file, but it is actually the third column in the salaries.txt file?

If you run a basic join command, it will fail because it is blindly looking at the first column of both files. You must explicitly tell the command exactly which columns to anchor to.

You use the -1 flag to specify the column for the first file, and the -2 flag to specify the column for the second file.

join -1 1 -2 3 sorted_ids.txt sorted_salaries.txt

This tells Linux: “Anchor the first column of file one to the third column of file two.” The command will now successfully merge the data, ignoring the mismatched column order.

Step 4: Handling Missing Data

By default, the join command is ruthless. If Employee 105 exists in the ID file, but their salary data is completely missing from the second file, the join command will completely delete Employee 105 from the final output. It only prints perfect matches.

If you want to force the command to print everyone in the first file, even if they have no matching data in the second file (so you can easily see who is missing their salary data), use the -a (unpairable) flag.

join -a 1 sorted_ids.txt sorted_salaries.txt

The -a 1 tells Linux to forcefully print all unpairable lines from File 1. The output will now include “105 John Doe,” highlighting the exact missing data gap in your system.

Step 5: Saving the Report

Just like most Linux text tools, join only prints to the screen. To save your perfectly merged corporate report, simply use the redirection bracket (>) to push the output into a brand new file.

join sorted_ids.txt sorted_salaries.txt > final_report.txt

By mastering the join command, you effectively turn the Linux terminal into a lightning-fast, highly capable relational database, allowing you to manipulate raw text files with incredible precision.

Get the best tech tips delivered straight to your inbox.

Join thousands of readers mastering Apple, Google, Microsoft, and Linux.