CLI tool that can execute SQL queries on CSV, LTSV, JSON, YAML and TBLN. Can output to various formats.
CLI tool that can execute SQL queries on CSV, LTSV, JSON, YAML and TBLN. Can output to various formats.
trdsql is a CLI tool that can execute SQL queries on CSV, LTSV, JSON, YAML and TBLN files.
This tool is similar to others such as q and textql, with a key distinction: it allows the use of PostgreSQL or MySQL syntax.
For usage as a library, please refer to the godoc and the provided examples.
go get -d github.com/noborus/trdsql
cd $GOPATH/src/github.com/noborus/trdsql
make
make installgo 1.25 or higher.
Download binary from the releases page(Linux/Windows/macOS).
brew install trdsqlsudo port selfupdate
sudo port install trdsqlpkg install trdsqlTypically, go-sqlite3 is used for building.
However, if you're building with CGO_ENABLED=0, consider using sqlite instead.
Building without CGO (CGO Free) can reduce issues related to cross-compiling, but it may result in slower execution times.
Pull the latest image from the Docker hub.
docker pull noborus/trdsqlOr build it yourself.
docker build -t trdsql .Docker run.
docker run --rm -it -v $(pwd):/tmp trdsql [options and commands]To use trdsql, you can either specify an SQL query or simply provide a file for conversion.
trdsql [options] SQLFor file conversion, this is equivalent to executing 'trdsql -o[output format] "SELECT * FROM filename"'.
trdsql -o[output format] -t [input filename]-a filename analyze the file and suggest SQL.-A filename analyze the file but only suggest SQL.-config filename configuration file location.-db db name specify db name of the setting.-dblist display db list of configure.-driver string database driver. [ mysql | postgres | sqlite3 | sqlite(CGO Free) ] (default "sqlite3")-dsn string database driver specific data source name.-debug debug print.-help display usage information.-q filename read query from the specified file.-t filename read table name from the specified file.-version display version information.-ig guess format from extension. (default)-icsv CSV format for input.-ijson JSON format for input.-iltsv LTSV format for input.-iyaml YAML format for input.-itbln TBLN format for input.-iwidth width specification format for input.-itext text format for input.-ilogfmt logfmt format for input.-ih the first line is interpreted as column names(CSV only).-id character field delimiter for input(default ",")(CSV only).-ijq string jq expression string for input(JSON/JSONL only).-ilr int limited number of rows to read.-inull string value(string) to convert to null on input.-inum add row number column.-ir int number of rows to preread. (default 1)-is int skip header row.-ocsv CSV format for output. (default)-otsv TSV format for output.-ojson JSON format for output.-ojsonl JSONL(JSON Lines) format for output.-oltsv LTSV format for output.-oat ASCII Table format for output.-omd Markdown format for output.-oraw Raw format for output.-ovf Vertical format for output.-oyaml YAML format for output.-otbln TBLN format for output.Or, guess the output format by file name.
-out filename output file name.-out-without-guess output without guessing (when using -out).-oh output column name as header.-od character field delimiter for output. (default ",")(CSV and RAW only).-oq character quote character for output. (default """)(CSV only).-oaq enclose all fields in quotes for output(CSV only).-ocrlf use CRLF for output. End each output line with '\r\n' instead of '\n'."(CSV only).-onowrap do not wrap long columns(AT and MD only).-onull value(string) to convert from null on output.-oz string compression format for output. [ gzip | bz2 | zstd | lz4 | xz ]NULL is undecided in many text formats.
JSON null is considered the same as SQL NULL.
For formats other than JSON, you must specify a string that is considered NULL.
In most cases you will need to specify an empty string ("").
If -inull "" is specified, an empty string will be treated as SQL NULL.
SQL NULL is an empty string by default. Specify the -onull "(NULL)" option if you want a different string.
$ echo "1,,v" | trdsql -inull "" -onull "(NULL)" "SELECT * FROM -"
1,(NULL),vIn the case of JSON, null is NULL as it is, and the specified string is converted to NULL.
$ echo '[1,null,""]' | trdsql -inull "" -ojson -ijson "SELECT * FROM -"
[
{
"c1": "1"
},
{
"c1": null
},
{
"c1": null
}
]Multiple queries can be executed by separating them with a semicolon. Update queries must be followed by a SELECT statement.
$ trdsql "UPDATE SET c2='banana' WHERE c3='1';SELECT * FROM test.csv"
1,Orange
2,Melon
3,bananaYou can perform multiple SELECTs, but the output will be in one format.
$ trdsql -oh "SELECT c1,c2 FROM test.csv;SELECT c2,c1 FROM test.csv"
c1,c2
1,Orange
2,Melon
3,Apple
c2,c1
Orange,1
Melon,2
Apple,3test.csv file.
1,Orange
2,Melon
3,ApplePlease write a file name like a table name.
trdsql "SELECT * FROM test.csv"-q filename can execute SQL from file
trdsql -q test.sql"-" or "stdin" is received from standard input instead of file name.
cat test.csv | trdsql "SELECT * FROM -"or
cat test.csv | trdsql "SELECT * FROM stdin"Multiple matched files can be executed as one table.
$ trdsql -ih "SELECT * FROM tt*.csv"
1,test1
2,test2
3,test3[!NOTE] It is not possible to mix different formats (ex: CSV and LTSV).
If the file is compressed with gzip, bz2, zstd, lz4, xz, it will be automatically uncompressed.
trdsql "SELECT * FROM testdata/test.csv.gz"trdsql "SELECT * FROM testdata/test.csv.zst"It is possible to mix uncompressed and compressed files using wildcards.
trdsql "SELECT * FROM testdata/test.csv*"-out filename option to output the file to a file.
trdsql -out result.csv "SELECT * FROM testdata/test.csv ORDER BY c1"-oz compression type to compress and output.
trdsql -oz gz "SELECT * FROM testdata/test.csv ORDER BY c1" > result.csv.gzThe filename of -out filename option determines
the output format(csv, ltsv, json, tbln, raw, md, at, vf, jsonl)
and compression format(gzip, bz2, zstd,lz4, xz) by guess.
Guess by extension output format + output compression (eg .csv.gz, .ltsv.lz4, .md.zst ...).
The following is an LTSV file compressed in zstd.
trdsql -out result.ltsv.zst "SELECT * FROM testdata/test.csv"If the number of columns is not a constant, read and decide multiple rows.
$ trdsql -ir 3 -iltsv "SELECT * FROM test_indefinite.ltsv"
1,Orange,50,,
2,Melon,500,ibaraki,
3,Apple,100,aomori,red-id "\\t" is input from TSV (Tab Separated Value)
1 Orange
2 Melon
3 Appletrdsql -id "\t" "SELECT * FROM test-tab.csv"-otsv is TSV (Tab Separated Value) output.
$ trdsql -otsv "SELECT * FROM test.csv"
1 Orange
2 Melon
3 Apple-iltsv is input from LTSV(Labeled Tab-separated Values).
sample.ltsv
id:1 name:Orange price:50
id:2 name:Melon price:500
id:3 name:Apple price:100trdsql -iltsv "SELECT * FROM sample.ltsv"1,Orange,50
2,Melon,500
3,Apple,100[!NOTE] Only the columns in the first row are targeted.
-oltsv is LTSV(Labeled Tab-separated Values) output.
$ trdsql -iltsv -oltsv "SELECT * FROM sample.ltsv"
id:1 name:Orange price:50
id:2 name:Melon price:500
id:3No open issues yet, or sync has not completed.