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.

3 Steps 1 min read Easy

Step-by-Step Guide

  1. 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
  2. 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
  3. 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

D

Donald Stokes

Experienced content creator specializing in practical skills guides and tutorials.

45 articles
View all articles

Rate This Guide

--
Loading...
5
0
4
0
3
0
2
0
1
0

How helpful was this guide? Click to rate: