Linux Commands - join (combine or merge lines in two different files)

by
Jeremy Canfield |
Updated: March 09 2020
| Linux Commands articles
The join command will join or merge similar lines in two different files. The join command without any options will combine lines that begin with an identical string until the first whitespace. For example, let's consider two files that contain lines that begin with an identical string (Employee).
# file1.txt
Employee name=John Doe
# file2.txt
Employee occupation=Engineer
The join command followed by the two files will join the lines that begin with the identical string.
~]# join file1.txt file2.txt
Employee name=John Doe occupation=Engineer
Not sorted
The lines in the files being joined must be sorted. If the files are not sorted, "not sorted" will be displayed when attempting to join the files.
~]# join file1.txt file2.txt
join: file1.txt is not sorted
Use the sort command to sort the files being joined.
~]# sort file1.txt -o file1.txt
~]# sort file2.txt -o file2.txt
~]# join file1.txt file2.txt
Employee name=John Doe occupation=Engineer
Employee name=Jane Doe occupation=Sales Manager
Did you find this article helpful?
If so, consider buying me a coffee over at