Skip to content
Latchkey

q: Run SQL Directly on Text and CSV

q lets you run standard SQL against ordinary text and CSV files, referring to stdin as the table - and using column positions like c1, c2 when there is no header.

q (harelba/q) is built for the command line: pipe any delimited text in, write a SELECT, and get rows out. It is the SQL answer to awk for log lines and exports.

What it does

q loads a delimited text file (or stdin, referenced as -) into an in-memory SQLite table and runs your SQL. Without -H, columns are named c1, c2, c3 by position; with -H the first row supplies names. Output delimiter and header are controlled separately from input.

Common usage

Terminal
ps aux | q -H "SELECT COMMAND, \"%CPU\" FROM - ORDER BY \"%CPU\" DESC LIMIT 5"
q -d, -H "SELECT name FROM users.csv WHERE age > 30"
# no header: reference columns by position
cat access.log | q "SELECT c1, COUNT(*) FROM - GROUP BY c1"

Options

FlagWhat it does
-HInput has a header row (name columns from it)
-d <char>Input delimiter (default whitespace)
-OPrint an output header row
-D <char>Output delimiter
-tTab-delimited shortcut for -d $'\t'

In CI

q turns command output into queryable data: ps aux | q -H "SELECT ... FROM -" reads the process table directly. When there is no header, columns are c1..cN, so a change in the input column order silently shifts them; prefer -H with named columns when the source has one.

Common errors in CI

"query error: no such column: cN" means you referenced a position past the actual column count, often because the delimiter was wrong (default is whitespace, so pass -d, for CSV). "Bad header row" or a mismatch appears when -H is set but rows have uneven field counts. Quote column names containing symbols like %CPU in double quotes for SQL.

Related guides

Run this faster and cheaper on Latchkey managed runners. Start free →