Skip to content

Instantly share code, notes, and snippets.

@sephraim
Last active November 11, 2016 22:46
Show Gist options
  • Save sephraim/cdad83e61cb3c4bc510d79804170fbd7 to your computer and use it in GitHub Desktop.
Save sephraim/cdad83e61cb3c4bc510d79804170fbd7 to your computer and use it in GitHub Desktop.
Join multiple CSVs / TSVs using SQLite
/**
* STEP 1: Write queries to import files into an SQLite database
**/
/* Set file input mode */
.mode tabs
/* Import tables from TSV files */
.import file1.tsv table1
.import file2.tsv table2
.import file3.tsv table3
/**
* STEP 2: Write queries to JOIN
**/
/* Set file output mode */
.mode csv
.headers on
/* OPTIONALLY */
/* .output output.csv */
/* JOIN tables together and output to CSV file */
SELECT a.*, b.column2, c.column2, c.column3, c.column4
FROM table1 a
LEFT OUTER JOIN table2 b ON a.column1 = b.column1
LEFT OUTER JOIN table3 c ON a.column1 = c.column1;
#!/bin/bash
# Run STEP 1
sqlite3 mydatabase.db < create_db.sql
# Run STEP 2
sqlite3 mydatabase.db < join.sql > output.csv
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment