Masotom
Masotom

Reputation: 11

Join two csv files

I need to merge these two sorted csv files on 'contract' column (which is common for both files):

file1.csv

data;client;contract

Mar 04 2017;LA00024;1-456

Mar 04 2017;LA00025;1-789

file2.csv

contract;PdA

1-456;00024

1-789;00025

The output I'd like to get it is a unique table:

data;client;contract;PdA

Mar 04 2017;LA00024;1-456;00024

Mar 04 2017;LA00025;1-789;00025

However using command

$ join -o 1.1 1.2 1.3 2.2 -t";" -1 3 -2 1 file1.csv file2.csv

The result I get is just a line

$ data;client;contract;PdA

The join doesn't return anything but the merge of the titles from input files? Why does it act like this?

Upvotes: 1

Views: 668

Answers (1)

franklinsijo
franklinsijo

Reputation: 18270

Sort the files before join.

join -o 1.1,1.2,1.3,2.2 -t ';' -1 3 -2 1 <(sort -n -t";" -k3 file1.csv) <(sort -n -t";" -k1 file2.csv)

PS: For the sample data, the command in question worked for me. Required , in -o FORMAT.

Upvotes: 1

Related Questions