Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/jolmg/cq
Query CSVs using SQL
https://github.com/jolmg/cq
Last synced: 1 day ago
JSON representation
Query CSVs using SQL
- Host: GitHub
- URL: https://github.com/jolmg/cq
- Owner: jolmg
- License: gpl-3.0
- Created: 2019-08-12T01:34:07.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2019-09-22T03:48:48.000Z (about 5 years ago)
- Last Synced: 2024-08-02T05:10:37.247Z (3 months ago)
- Language: Shell
- Homepage:
- Size: 60.5 KB
- Stars: 167
- Watchers: 5
- Forks: 5
- Open Issues: 4
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# cq
Wrapper over sqlite inspired by jq for querying CSVs using SQL## Examples of use
```
$ cat t/foobar.csv
foo,bar
1,2
$ cq t/foobar.csv -q 'select foo, bar + 10 as bar_2 from foobar;'
foo,bar_2
1,12
$ cat t/barbaz.csv
bar,baz
2,3
$ cq t/* -q 'select foo, baz from foobar, barbaz where foobar.bar = barbaz.bar;'
foo,baz
1,3
```The tablenames are the basenames of the files without their optional .csv extension. One can also provide explicit names for the tables in 2 ways. One is with prefix assignment:
```
$ cq f:=t/foobar.csv b:=t/barbaz.csv -q 'select foo, baz from f, b where f.bar = b.bar;'
foo,baz
1,3
```And the other one is with suffix assignment:
```
$ cq t/foobar.csv=:f t/barbaz.csv=:b -q 'select foo, baz from f, b where f.bar = b.bar;'
foo,baz
1,3
```Notice one uses `:=` and the other one is flipped `=:`. The former looks better when combining with <() process substitution:
```
cq \
a:=<(ssh server_a generate_csv) \
b:=<(ssh server_b produce_different_csv)
```The latter works better with brace expansion:
```
cq a/very/long/path/{long_filename=:a,elsewhere/another_file=:b}
```Output headers can be disabled with `+H`, and re-enabled with `-H`. The last option overrides the formers so you can set a default in a shell alias.
```
$ cq +H t/foobar.csv -q 'select foo, bar from foobar;'
1,2
```Output mode can be set with `-o`:
```
$ cq -o column t/foobar.csv -q 'select foo, bar from foobar;'
foo bar
---------- ----------
1 2
```Supported output modes can be listed with `sqlite3 <<< '.help mode'`.
There's a `--help` option for more.