Operations 7 min read

Linux join Command Tutorial: Merge Files by Common Fields with Examples

This tutorial explains the Linux join command for merging two files based on matching key fields, covering syntax, common options like -a, -e, -i, -o, -t, -v, -1, -2, and provides practical examples with sample file contents and command outputs.

Lakehouse Research Base
Lakehouse Research Base
Lakehouse Research Base
Linux join Command Tutorial: Merge Files by Common Fields with Examples

Introduction to the join Command

The join command in Linux merges lines from two files that share a common key field, similar to a SQL JOIN operation. It reads file1 and file2 and outputs paired lines where the join fields match.

Basic Syntax

join [options] file1 file2

Common Options

-a <1|2>

: Print unmatched lines from the specified file (1 or 2) in addition to matched lines. -e <string>: Replace missing output fields with the given string. -i or --ignore-case: Ignore case differences when comparing join fields. -o <format>: Define custom output format using field specifiers like 1.1, 2.2. -t <char>: Use the specified character as the field delimiter (default is whitespace). -v <1|2>: Print only unmatched lines from the specified file (suppress matched lines). -1 <field> and -2 <field>: Select the join field from file1 and file2 respectively (1‑based). --check-order (default): Verify that input files are sorted on the join key. --nocheck-order: Skip the sort‑order check.

Example Files

Two sample files are used throughout the examples:

file1.txt:
11 Alice
12 Bob
13 John
14 Jack

file2.txt:
11 Doctor
12 Engineer
13 Teacher
15 Lawyer
17 Teacher
19 Driver
20 Policeman

Example 1: Default Join

Running join file1.txt file2.txt joins on the first field of each file (default). Only lines with matching keys (11, 12, 13) appear in the output:

11 Alice Doctor
12 Bob Engineer
13 John Teacher

Example 2: Specifying Join Fields

Explicitly joining on the first field of both files: join -1 1 -2 1 file1.txt file2.txt Produces the same result as the default join because the first fields already match.

Attempting to join on the second field ( join -1 2 -2 2 file1.txt file2.txt) fails because the second fields (names vs. professions) share no common values.

When the files are extended with a third column (gender) and an attempt is made to join on that third field ( join -1 3 -2 3 file1.txt file2.txt), the command reports sort‑order errors:

join: file1.txt:2: is not sorted: 12 Bob F
join: file2.txt:7: is not sorted: 20 Policeman F

The error occurs because the third column values are not sorted lexicographically (A–Z). After sorting both files on the third field (e.g., with sort -k3), the join succeeds and matches lines where the gender field is identical.

Example 3: Printing Unmatched Lines with -a

Command: join -a 1 file1.txt file2.txt Output includes all lines from file1; for key 14 (Jack) which has no match in file2, the missing fields are left empty:

11 Alice Doctor
12 Bob Engineer
13 John Teacher
14 Jack

Example 4: Custom Empty‑Field Replacement with -e

Command: join -e xxxx file1.txt file2.txt Unmatched fields are filled with the string xxxx instead of being left blank:

11 Alice Doctor
12 Bob Engineer
13 John Teacher
14 Jack xxxx

Example 5: Case‑Insensitive Matching with -i

If the join fields differ only in case, adding -i enables matching. For instance, joining on the third field (gender) with mixed case:

join -1 3 -2 3 file1.txt file2.txt          # case‑sensitive, may miss matches
join -i -1 3 -2 3 file1.txt file2.txt       # case‑insensitive, matches 'M' with 'm', 'F' with 'f'

The case‑insensitive version produces additional matched lines that the case‑sensitive version would skip.

Summary

The join command is a powerful text‑processing tool for relational‑style merging of sorted files. Key takeaways:

Input files must be sorted on the join key unless --nocheck-order is used.

Use -1 and -2 to select different join fields in each file.

Options -a and -v control inclusion of unmatched lines. -e provides a placeholder for missing fields; -i enables case‑insensitive comparison.

Combining these options allows flexible data‑integration workflows directly from the shell.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

data processingLinuxcommand-linetext processingshell scriptingfile mergingjoin command
Lakehouse Research Base
Written by

Lakehouse Research Base

Focused on technical sharing in the data field, covering a tech stack that includes Hadoop, Spark, Flink, Kafka, Fluss, Paimon, Iceberg, StarRocks, ClickHouse, ES, Milvus, and more. Welcome to follow.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.