So I have 2 different files.

file1.txt is:


file2.txt is:

*other random lines which do not occur in file1.txt also and are not needed

How can I print them on the same line so that they are like:


I'm going to assume you want the second line merged as well. You can use join:

join -t : file1 file2

This parses file1 and file2 using : as separator, and merges lines whose first field matches. By default lines which don't match are ignored and don't appear in the output.

The input files need to be sorted on the join field; if they're not, you can pre-process them:

join -t : <(sort -k 1,1 -t : file1) <(sort -k 1,1 -t : file2)

or you can try ignoring the sort check:

join -t : --nocheck-order file1 file2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.