How to Merge Files by a Common Field Using join in Linux

When you are managing massive, unstructured text databases on a Linux server (e.g., CSV dumps or raw log files), you may need to mathematically merge data from two entirely separate files based on a shared, identical field. Attempting to write a complex bash script using awk or sed to execute this relational merge is highly inefficient. To force the Linux kernel to execute a pristine, SQL-style “inner join” on two raw text files, you must use the join command.

Understanding the Relational Architecture

The join command is a deeply specialized text processing engine. It ingests two separate files, algorithmically scans them for a common mathematical “key” (a shared field or column), and outputs a completely new, merged line containing the data from both files.

CRITICAL INFRASTRUCTURE WARNING: The join engine is mathematically rigid. It absolutely demands that both target files are perfectly, alphabetically sorted on the key field before execution. If the files are unsorted, the engine will violently crash or output corrupted data.

Executing the Relational Merge

Imagine you have two files. names.txt contains employee IDs and names (e.g., 101 John). departments.txt contains employee IDs and their assigned department (e.g., 101 Engineering). Both files are perfectly sorted by the ID column.

To merge these two files based on their shared ID, open your terminal and type:

join names.txt departments.txt

The exact millisecond you press Enter, the engine scans the files. Because the default behavior is to use the very first column (Field 1) as the relational key, it detects “101” in both files. It then violently merges the data, outputting: 101 John Engineering.

Overriding the Relational Key

If the shared key is not in the first column, you must mathematically instruct the engine where to look. If the key is in column 2 of the first file, and column 3 of the second file, you execute:

join -1 2 -2 3 file1.txt file2.txt

The -1 flag dictates the column for the first file, and the -2 flag dictates the column for the second file. This allows you to execute highly complex relational merges across vastly different database structures.

Get the best tech tips delivered straight to your inbox.

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