FreeKB - Combine or merge lines in two different files using the JOIN command in Linux
Combine or merge lines in two different files using the JOIN command in Linux

Home > Search > Linux commands


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

 



Add a Comment




We will never share your name or email with anyone. Enter your email if you would like to be notified when we respond to your comment.




Please enter in the box below so that we can be sure you are a human.




Comments