join Command — Joining Files Using a Common Field¶
The
joincommand combines lines from two sorted files based on a common field, much like an SQL INNER JOIN. It is commonly used to merge related datasets such as employee records, inventory lists, configuration files, and reports. Understandingjoinis valuable for Linux administrators, DevOps engineers, and anyone working with structured text data.
Learning Path¶
Course Progress
What You'll Learn¶
After completing this lesson, you'll be able to:
- Understand the
joincommand - Join files using a common field
- Join using different columns
- Change field delimiters
- Display selected fields
- Handle unmatched records
- Use
joinin production scenarios
Prerequisites¶
Before starting this lesson, complete:
- Module 1 – Linux Fundamentals
- Module 2 – Linux Command Line Essentials
- Module 3 Lessons 1–8
Why Learn join?¶
Suppose you have two files.
employees.txt
departments.txt
You want this result:
Instead of manually combining them, use:
What is join?¶
The join command combines two files based on a matching field.
It works similarly to an SQL INNER JOIN.
Syntax:
Important
Both files must be sorted on the join field before using join.
Sample Files¶
Create:
Contents:
Create:
Contents:
Verify Sorting¶
Before joining:
Or sort in-place.
Basic Join¶
Output:
How join Works¶
employees.txt
101 Alice
102 Bob
↓
Match on Field 1
↓
departments.txt
101 Engineering
102 HR
↓
Joined Output
Join Using a Different Delimiter¶
Suppose the files are CSV.
employees.csv
departments.csv
Command:
Output:
Join on Different Fields¶
Suppose:
employees.txt
departments.txt
Join using:
Explanation:
-1 2→ Use field 2 from File 1-2 1→ Use field 1 from File 2
Display Selected Fields¶
Default output:
Customize output.
Output:
Include Unmatched Records¶
Normally:
Only matching records appear.
To include unmatched records from File 1:
Include unmatched from File 2:
Include all records:
Replace Missing Values¶
Display "N/A" for missing matches.
Ignore Case¶
If the join field differs only by letter case:
Common join Options¶
| Option | Description |
|---|---|
-t | Specify delimiter |
-1 | Join field from File 1 |
-2 | Join field from File 2 |
-o | Select output fields |
-a1 | Include unmatched lines from File 1 |
-a2 | Include unmatched lines from File 2 |
-e | Replace missing fields |
-i | Ignore case |
Combining with Other Commands¶
Sort before joining.
sort employees.txt -o employees.txt
sort departments.txt -o departments.txt
join employees.txt departments.txt
Extract usernames and shells.
Difference Between paste and join¶
| paste | join |
|---|---|
| Combines files by line number | Combines files by matching field |
| Files need equal line order | Files must be sorted |
| Similar to ZIP | Similar to SQL JOIN |
Real Production Examples¶
Merge server inventory.
Command:
Merge user accounts.
Merge cloud inventory.
Merge Kubernetes reports.
Production Perspective¶
The join command is useful for:
- Combining inventory reports
- Merging user information
- Creating audit reports
- Processing CSV data
- Infrastructure automation
- Reporting and analytics
Hands-on Lab¶
Task 1¶
Create:
Contents:
Task 2¶
Create:
Contents:
Task 3¶
Sort both files.
Task 4¶
Join them.
Task 5¶
Display only names and marks.
Task 6¶
Convert to CSV.
Task 7¶
Add an unmatched record.
Use:
Task 8¶
Replace missing marks.
Command Deep Dive¶
| Command | Purpose | Production Example |
|---|---|---|
join file1 file2 | Join files | Reports |
join -t "," | CSV files | Inventory |
join -1 | Different field | User mapping |
join -o | Custom output | Reports |
join -a1 | Include unmatched | Audits |
join -e | Replace missing values | Compliance reports |
Production Troubleshooting Scenario¶
Scenario
A cloud administrator has two reports.
instances.txt
billing.txt
Tasks:
- Merge both reports.
- Display VM names and monthly cost.
- Include VMs without billing records.
- Export the final report as CSV.
Solutions:
join instances.txt billing.txt
join -o 1.1,2.2 instances.txt billing.txt
join -a1 instances.txt billing.txt
join instances.txt billing.txt | tr ' ' ','
Mini Challenge¶
Create:
employees.txt
salary.txt
Perform the following:
- Sort both files.
- Join them.
- Display only names and salaries.
- Add an unmatched employee.
- Replace missing salary with
N/A. - Export the report as CSV.
Best Practices¶
- Always sort files before using
join. - Verify that the join field is identical in both files.
- Use
-twhen working with CSV files. - Use
-oto generate clean reports. - Use
-a1and-a2when missing records are important.
Common Mistakes¶
❌ Joining unsorted files.
✅ Incorrect:
If the files are not sorted, join may fail or produce incomplete output.
Correct:
sort employees.txt -o employees.txt
sort departments.txt -o departments.txt
join employees.txt departments.txt
❌ Confusing paste with join.
✅ - paste joins by line number. - join joins by matching field.
❌ Using different delimiters.
✅ If one file uses commas and another uses spaces, normalize the data first.
Interview Questions¶
Beginner¶
- What does the
joincommand do? - Why must files be sorted before using
join? - What does
-tspecify? - What is the purpose of
-o?
Intermediate¶
- Explain
join -1andjoin -2. - What does
join -a1do? - How do you replace missing values?
- Difference between
joinandpaste?
Architect Level¶
- How would you combine multiple infrastructure reports using
join? - Why is
joinsimilar to a database JOIN? - How would you automate inventory report generation using
sort,join, andawk?
Summary¶
In this lesson, you learned:
- Joining files by a common field
- Working with sorted files
- Joining CSV files
- Selecting output columns
- Handling unmatched records
- Production reporting techniques
The join command is a powerful utility for combining structured data from multiple sources. It is especially valuable for generating reports, merging inventories, and automating administrative tasks.
Key Takeaways¶
joincombines files using a matching field.- Both files should be sorted on the join field.
- Use
-tfor custom delimiters. - Use
-oto customize output. - Use
-a1and-a2to include unmatched records. - Think of
joinas the Linux equivalent of an SQL INNER JOIN.
What's Next?¶
split Command — Splitting Large Files in Linux
In the next lesson, you'll learn:
- Splitting files by size
- Splitting files by number of lines
- Custom file prefixes and suffixes
- Reassembling split files
- Handling large log files and backups