Take the 2-minute tour ×
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.

I have a 2 files with the following:

File1.txt 
A 1
B 2
C 5
Z 3

File2.txt
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

2 Answers 2

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 at 17:18
    
@StenKin, yes, the inputs must be sorted on the join keys (here the first column). –  Stéphane Chazelas Aug 4 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

 
discard

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.