The slightly cryptic string -12jo
refers to the four separate options -1
, -2
, -j
and -o
, of which the first three has to do with selecting what field in each file to join on and the last has to do with what fields from each file should be outputted. The -j
option is an extension in GNU join
and and -j n
is the same as -1 n -2 n
(where n
is some integer).
The -e
option comes into effect when you, with -a
, request to get unpaired lines from one or both of the files that you join. An unpaired line will have missing data, as the line from one file did not correspond to a line in the other file. The -e
option replaces those fields with the given string. Likewise, if you request, with -o
, a field that does not exist on a particular line in a file, you would use -e
to replace the empty values with a string.
Example: Two files that contain manufacturing costs and sales income for a number of products. Each file has the fields
- Product ID
- Product name
- Some number
$ cat expenses.txt
1 teacup 5
2 spoon 7
3 bowl 10
$ cat sales.txt
1 teacup 30
2 spoon 24
To get the expenses and sales for all products, while replacing the number (from either the first or second file) that may be missing with the string NONE
, I would do
$ join -a1 -a2 -o0,1.2,1.3,2.3 -e NONE expenses.txt sales.txt
1 teacup 5 30
2 spoon 7 24
3 bowl 10 NONE
Here, I use the -a
option twice to request all lines from both files (a "full outer join" in SQL speak). The -o
option is used to get specific fields from each file (field 0
is the join field, which is the first field in each file by default), and -e
to specify the string NONE
to replace missing value with.
As you can see, we get NONE
as the "sales value" since the product with ID 3 was not mentioned in that second file.
-a
.join -o 1.2 -e NONE <(echo a) <(echo a)
would also outputNONE
– Stéphane Chazelas Jul 24 '18 at 06:59join -a 1 -a 2 -t " " -e "NULL" -1 1 -2 1 <(sort file1) <(sort file2)
not output NULL, supposefile2
has unpairable line? – Tim Jul 24 '18 at 13:21file1
andfile2
are here https://unix.stackexchange.com/q/458068/674 – Tim Jul 24 '18 at 13:27join -a 2 -o0,1.2,2.2 -t " " -e "NULL" <(sort file1) <(sort file2)
– Kusalananda Jul 24 '18 at 13:31-e
work? Does-e
only work with-o
? – Tim Jul 24 '18 at 13:33join
does not output missing values unless you explicitly request those fields with-o
. Missing values will then be blank, unless you use-e
. – Kusalananda Jul 24 '18 at 13:35