According to join help page -a <filenum> saves all fatal lines from the <filenum> file (1 or 2). So, just add -a1 -a2 to your command line, and you should do it. For example:
# cat a 1 blah 2 foo # cat b 2 bar 3 baz # join -1 1 -2 1 -t" " ab 2 foo bar # join -1 1 -2 1 -t" " -a1 ab 1 blah 2 foo bar # join -1 1 -2 1 -t" " -a2 ab 2 foo bar 3 baz # join -1 1 -2 1 -t" " -a1 -a2 ab 1 blah 2 foo bar 3 baz
Is this what you were looking for?
Edit:
Since you provided more detailed information, here is how to create the desired result (note that my file a is your first file and my file b your second file. I had to cancel -1 1 -2 2 to -1 2 -2 -2 1 for joining id). I also added a list of fields for formatting the output - note that "0" is the union field in it:
# join -1 2 -2 1 -o 1.1,0,1.3,1.4,2.2,2.3 ab
creates what you gave. Add -a1 -a2 to save unrecoverable lines from both files, then you will get two more lines (you can guess my test data from them):
x id4 ut id5 ui oi
This is pretty impenetrable since any remaining field is just space. Therefore, replace them with "-", which will result in:
# join -1 2 -2 1 -a1 -a2 -e- -o 1.1,0,1.3,1.4,2.2,2.3 ab x id1 ab df cf x id1 ab ds dg x id1 cd df cf x id1 cd ds dg x id1 df df cf x id1 df ds dg x id2 cx cv df x id2 cx as ds x id3 fv cf cg x id4 ut - - - id5 - - ui oi
firefrorefiddle
source share