Last active
November 11, 2016 22:46
-
-
Save sephraim/cdad83e61cb3c4bc510d79804170fbd7 to your computer and use it in GitHub Desktop.
Join multiple CSVs / TSVs using SQLite
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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