Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a 2 files with the following:

A 1
B 2
C 5
Z 3

A 4
B 7 
C 10
D 11

What I would like to do is create something like

A 1 4 
B 2 7
C 5 10
D - 11
Z 3 -

Is there a utility that does this? If not how Can this be done? Using a find and awk or something?

share|improve this question
up vote 8 down vote accepted
join -a1 -a2 -o 0,1.2,2.2 -e - file1.txt file2.txt
share|improve this answer
Does This require that the files be sorted in order? – Sten Kin Aug 4 '14 at 17:18
@StenKin, yes, the inputs must be sorted on the join keys (here the first column). – Stéphane Chazelas Aug 4 '14 at 20:00
$ join File1.txt File2.txt

almost does what you want - but it only displays lines which have a matching key.

It's easy enough to script the result you want, but I don't know a single utility that does it exactly.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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