Skip to content

Instantly share code, notes, and snippets.

@knbknb
Last active June 21, 2022 11:28
Show Gist options
  • Save knbknb/f461537c92cd34703e22ac933e6837fa to your computer and use it in GitHub Desktop.
Save knbknb/f461537c92cd34703e22ac933e6837fa to your computer and use it in GitHub Desktop.
sqlite3 to parse csv files
#!/usr/bin/env bash
## Christian Ştefănescu @stchris_ 2022 Jun 17
## Handy pattern to put metadata from photos into a #sqlite database for easy querying:
exiftool -csv *.JPG > photos.csv
## Convert the csv to sqlite3
sqlite3 photos.db -cmd \
".mode csv" ".import photos.csv photos" "select filename, imagewidth, imageheight from photos;" ".quit"
## You can do the same thing with my sqlite-utils tool,
## it's just MASSIVELY slower
## (due to parsing the CSV in Python as opposed to SQLite's optimized C parser)
## expected output: (often full with NULLS returned as "")
# IMG_0976.JPG,2048,1536
# LACK-reagl-gfz.JPG,3264,2448
## this syntax is how you read from stdin:
# -cmd ".import '|cat -' mytable"
## You also have process redirection in bash and zsh:
# sqlite3 … “.import <( jq … ) jqtable”
# duckdb -c 'select a,sum(b) from "/tmp/a.csv" group by a;'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment