2016-11-05 13:39:47 -05:00
# q
2024-03-14 02:01:06 -03:00
> Execute SQL-like queries on CSV and TSV files.
2019-05-23 11:56:50 +01:00
> More information: <https://harelba.github.io/q>.
2016-11-05 13:39:47 -05:00
2024-03-14 02:01:06 -03:00
- Query a CSV file by specifying the delimiter as ',':
2016-11-05 13:39:47 -05:00
2025-04-22 06:20:48 +03:00
`q {{[-d|--delimiter]}} ',' "SELECT * from {{path/to/file}}"`
2016-11-05 13:39:47 -05:00
2024-03-14 02:01:06 -03:00
- Query a TSV file:
2016-11-05 13:39:47 -05:00
2025-04-22 06:20:48 +03:00
`q {{[-t|--tab-delimited]}} "SELECT * from {{path/to/file}}"`
2016-11-05 13:39:47 -05:00
- Query file with header row:
2025-04-22 06:20:48 +03:00
`q {{[-d|--delimiter]}} {{delimiter}} {{[-H|--skip-header]}} "SELECT * from {{path/to/file}}"`
2016-11-05 13:39:47 -05:00
2022-12-04 08:53:34 +01:00
- Read data from `stdin` ; '-' in the query represents the data from `stdin` :
2016-11-05 13:39:47 -05:00
`{{output}} | q "select * from -"`
- Join two files (aliased as `f1` and `f2` in the example) on column `c1` , a common column:
`q "SELECT * FROM {{path/to/file}} f1 JOIN {{path/to/other_file}} f2 ON (f1.c1 = f2.c1)"`
2025-05-04 05:15:17 +03:00
- Format output using an output delimiter with an output header line (Note: Command will output column names based on the input file header or the column aliases overridden in the query):
2016-11-05 13:39:47 -05:00
2025-04-22 06:20:48 +03:00
`q {{[-D|--output-delimiter]}} {{delimiter}} {{[-O|--output-header]}} "SELECT {{column}} as {{alias}} from {{path/to/file}}"`