Handling Unmatched Records
In real-world scenarios, you might have records in one file that don't have corresponding matches in the other file. Let's simulate this by adding a new employee who hasn't been assigned a salary yet.
Add this employee to employees.txt
:
echo "1006 Alex IT" >> employees.txt
The >>
operator appends the new line to the end of the file without overwriting existing content.
Now, if we run our basic join command:
join employees.txt salaries.txt
You'll notice that Alex doesn't appear in the output because there's no matching record in salaries.txt
.
To include unmatched records, we can use the -a
option:
join -a 1 employees.txt salaries.txt
Now you should see Alex in the output:
1001 John Engineering 75000
1002 Sarah Marketing 65000
1003 Mike Sales 70000
1004 Emily HR 60000
1005 David Finance 80000
1006 Alex IT
The -a 1
option tells join
to include unpairable lines from the first file (employees.txt). This is useful when you want to see all records from one file, even if they don't have matches in the other file.