join lines of two files on a common field
References
man join
join [OPTION]... FILE1 FILE2-a FILENUMalso print unpairable lines from file FILENUM, where FILENUM is 1 or 2, corresponding to FILE1 or FILE2-e EMPTYreplace missing input fields with EMPTY-i, --ignore-caseignore differences in case when comparing fields-j FIELDequivalent to '-1 FIELD -2 FIELD'-o FORMATobey FORMAT while constructing output line-t CHARuse CHAR as input and output field separator-v FILENUMlike -a FILENUM, but suppress joined output lines-1 FIELDjoin on this FIELD of file 1-2 FIELDjoin on this FIELD of file 2--check-ordercheck that the input is correctly sorted, even if all input lines are pairable--nocheck-orderdo not check that the input is correctly sorted--headertreat the first line in each file as field headers, print them without trying to pair them-z, --zero-terminatedend lines with 0 byte, not newline
Reference : https://shapeshed.com/unix-join/
Sample 1
$ cat foodtypes
1 Protein
2 Carbohydrate
3 Fat
$ cat foods
1 Cheese
2 Potato
3 ButterJoin
$ join foodtypes foods
1 Protein Cheese
2 Carbohydrate Potato
3 Fat ButterSample 2
$ cat wine
Red Beaunes France
White Reisling Germany
Red Riocha Spain
$ cat reviews
Beaunes Great!
Reisling Terrible!
Riocha MehJoined by Sepcified fields
$ join -1 2 -2 1 wine reviews
Beaunes Red France Great!
Reisling White Germany Terrible!
Riocha Red Spain MehSample 3
$ cat wine
White Reisling Germany
Red Riocha Spain
Red Beaunes France
$ cat reviews
Riocha Meh
Beaunes Great!
Reisling Terrible!Wrong
$ join -1 2 -2 1 wine reviews
join: wine:3: is not sorted: Red Beaunes France
join: reviews:2: is not sorted: Beaunes Great!
Riocha Red Spain Meh
Beaunes Red France Great!Correct
$ join -1 2 -2 1 <(sort -k 2 wine) <(sort reviews)
Beaunes Red France Great!
Reisling White Germany Terrible!
Riocha Red Spain MehSample 4
- An example is a CSV file where the separator is
,.
$ cat names.csv
1,John Smith,London
2,Arthur Dent, Newcastle
3,Sophie Smith,London
$ cat transactions.csv
£1234,Deposit,John Smith
£4534,Withdrawal,Arthur Dent
£4675,Deposit,Sophie SmithSpecify Separator
$ join -1 2 -2 3 -t , names.csv transactions.csv
John Smith,1,London,£1234,Deposit
Arthur Dent,2, Newcastle,£4534,Withdrawal
Sophie Smith,3,London,£4675,DepositUse Sample 4 above
$ join -1 2 -2 3 -t , -o 1.1,1.2,1.3,2.2,2.1 names.csv transactions.csv
1,John Smith,London,Deposit,£1234
2,Arthur Dent, Newcastle,Withdrawal,£4534
3,Sophie Smith,London,Deposit,£4675