I often get data in various CSV files. Typically, I just want to query them for specific stuff.
For smallish datasets, I would typically open the CSV with a Spreadsheet app and then work with the data from there.
A couple of years back I figured out I could easily use SQLite instead.
It is very easy to import a CSV file into SQLite and just query it from there.
Here is an example:
$ sqlite3 :memory: <<EOF
.import --csv /tmp/data.csv mytable
SELECT AVG(value) FROM mytable;
EOF
Result:
60.2222222222222
What this command does is to start SQLite with an
In-Memory database.
Then it runs the command
.import --csv /tmp/data.csv mytable
to import /tmp/data.csv
directly into a table.
Because the CSV file contains column headers, the table is created automatically.
Had the CSV file not contained headers, we would need to create the table manually using:
CREATE TABLE mytable (id TEXT, value INTEGER);
SELECT AVG(value) FROM mytable;
calculates the average of the values in the column value
.