Perform SQL queries in CSV

1 min. read

Caution! This article is 7 years old. It may be obsolete or show old techniques. It may also still be relevant, and you may find it useful! So it has been marked as deprecated, just in case.

Today I discovered a command-line tool that allows you to do SQL queries in a CSV file. It has a name that is very difficult to google search, not to mention to even click a link to it: q. Mwehehe.

I used it recently to clean a CSV file of shipping information that had invalid postcodes:

q -d, -O -H "SELECT DISTINCT * FROM shipping/data.csv WHERE year = 2015 AND postcode != 'Invalid'" > shipping/data2015.csv
  • -d is the field delimiter, in this case a comma.
  • -O keeps the header line in the output.
  • Finally, -H skips the header row.