How to Join Two Files on a Common Field
Identify the two files you will sort before joining., Sort both files on the field you will use to join the files., Join the two sorted files together.
Step-by-Step Guide
-
Step 1: Identify the two files you will sort before joining.
For example:
File One Name: customer_info.csv Layout: customer_number,name,favorite_store,email_address File Two Name: customer_transactions.csv Layout: customer_number,total_purchase_number,total_purchase_amount -
Step 2: Sort both files on the field you will use to join the files.
In this case, choose the field corresponding to "customer_number" in each file. $ sort
-t,
-k1,1 customer_info.csv > customer_info_sorted.csv $ sort
-t,
-k3,3 customer_transactions.csv > customer_transactions_sorted.csv , $ join
-t,
-1 1
-2 3 customer_info_sorted.csv customer_transactions_sorted.csv > customer_join.csv -
Step 3: Join the two sorted files together.
Detailed Guide
For example:
File One Name: customer_info.csv Layout: customer_number,name,favorite_store,email_address File Two Name: customer_transactions.csv Layout: customer_number,total_purchase_number,total_purchase_amount
In this case, choose the field corresponding to "customer_number" in each file. $ sort
-t,
-k1,1 customer_info.csv > customer_info_sorted.csv $ sort
-t,
-k3,3 customer_transactions.csv > customer_transactions_sorted.csv , $ join
-t,
-1 1
-2 3 customer_info_sorted.csv customer_transactions_sorted.csv > customer_join.csv
About the Author
Donald Stokes
Experienced content creator specializing in practical skills guides and tutorials.
Rate This Guide
How helpful was this guide? Click to rate: