{"id":13535956,"url":"https://github.com/noborus/trdsql","last_synced_at":"2025-05-13T20:22:09.533Z","repository":{"id":37765239,"uuid":"96136924","full_name":"noborus/trdsql","owner":"noborus","description":"CLI tool that can execute SQL queries on CSV, LTSV, JSON, YAML and TBLN. Can output to various formats.","archived":false,"fork":false,"pushed_at":"2025-04-23T01:56:39.000Z","size":3473,"stargazers_count":2090,"open_issues_count":13,"forks_count":77,"subscribers_count":19,"default_branch":"master","last_synced_at":"2025-04-28T11:57:16.892Z","etag":null,"topics":["cli","csv","go","golang","json","jsonl","ltsv","mysql","postgresql","sql","sqlite3","tbln","trdsql","yaml"],"latest_commit_sha":null,"homepage":"https://noborus.github.io/trdsql/","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/noborus.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null},"funding":{"github":"noborus"}},"created_at":"2017-07-03T17:55:52.000Z","updated_at":"2025-04-23T06:34:02.000Z","dependencies_parsed_at":"2023-09-24T17:39:27.594Z","dependency_job_id":"caf5526e-d131-4767-8bdc-62ea51aa0a58","html_url":"https://github.com/noborus/trdsql","commit_stats":{"total_commits":617,"total_committers":11,"mean_commits":56.09090909090909,"dds":"0.27228525121555913","last_synced_commit":"ca2b8054d0a76ab416519b553e22e3022d84b20f"},"previous_names":[],"tags_count":63,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/noborus%2Ftrdsql","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/noborus%2Ftrdsql/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/noborus%2Ftrdsql/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/noborus%2Ftrdsql/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/noborus","download_url":"https://codeload.github.com/noborus/trdsql/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":251311332,"owners_count":21569008,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["cli","csv","go","golang","json","jsonl","ltsv","mysql","postgresql","sql","sqlite3","tbln","trdsql","yaml"],"created_at":"2024-08-01T09:00:32.644Z","updated_at":"2025-04-28T11:57:28.329Z","avatar_url":"https://github.com/noborus.png","language":"Go","readme":"# trdsql\n\n[![PkgGoDev](https://pkg.go.dev/badge/github.com/noborus/trdsql)](https://pkg.go.dev/github.com/noborus/trdsql)\n[![Go Report Card](https://goreportcard.com/badge/github.com/noborus/trdsql)](https://goreportcard.com/report/github.com/noborus/trdsql)\n[![Go Coverage](https://github.com/noborus/trdsql/wiki/coverage.svg)](https://raw.githack.com/wiki/noborus/trdsql/coverage.html)\n[![GitHub Actions](https://github.com/noborus/trdsql/workflows/Go/badge.svg)](https://github.com/noborus/trdsql/actions)\n\ntrdsql is a CLI tool that can execute SQL queries on [CSV](https://tools.ietf.org/html/rfc4180),\n [LTSV](http://ltsv.org/), [JSON](https://tools.ietf.org/html/rfc7159), [YAML](https://yaml.org/) and [TBLN](https://tbln.dev/) files.\n\nThis tool is similar to others such as [q](https://github.com/harelba/q) and [textql](https://github.com/dinedal/textql), with a key distinction: it allows the use of PostgreSQL or MySQL syntax.\n\nFor usage as a library, please refer to the [godoc](https://pkg.go.dev/github.com/noborus/trdsql) and the provided examples.\n\n![trdsql.gif](https://raw.githubusercontent.com/noborus/trdsql/master/doc/trdsql.gif)\n\n\u003c!-- vscode-markdown-toc --\u003e\n* 1. [INSTALL](#install)\n  * 1.1. [go get](#go-get)\n    * 1.1.1. [Requirements](#requirements)\n  * 1.2. [Download binary](#download-binary)\n  * 1.3. [Homebrew](#homebrew)\n  * 1.4. [MacPorts](#macports)\n  * 1.5. [FreeBSD](#freebsd)\n  * 1.6. [Cgo free](#cgo-free)\n* 2. [Docker](#docker)\n  * 2.1. [Docker pull](#docker-pull)\n  * 2.2. [image build](#image-build)\n  * 2.3. [Docker Run](#docker-run)\n* 3. [Usage](#usage)\n  * 3.1. [Global options](#global-options)\n  * 3.2. [Input formats](#input-formats)\n    * 3.2.1. [Input options](#input-options)\n  * 3.3. [Output formats](#output-formats)\n    * 3.3.1. [Output options](#output-options)\n  * 3.4. [Handling of NULL](#handling-of-null)\n  * 3.5. [Multiple queries](#multiple-queries)\n* 4. [Example](#example)\n  * 4.1. [STDIN input](#stdin-input)\n  * 4.2. [Multiple files](#multiple-files)\n  * 4.3. [Compressed files](#compressed-files)\n  * 4.4. [Output file](#output-file)\n  * 4.5. [Output compression](#output-compression)\n  * 4.6. [Guess by output file name](#guess-by-output-file-name)\n  * 4.7. [Columns is not constant](#columns-is-not-constant)\n  * 4.8. [TSV (Tab Separated Value)](#tsv-(tab-separated-value))\n  * 4.9. [LTSV (Labeled Tab-separated Values)](#ltsv-(labeled-tab-separated-values))\n  * 4.10. [JSON](#json)\n    * 4.10.1. [jq expression](#jq-expression)\n  * 4.11. [JSONL(NDJSON)](#jsonl(ndjson))\n  * 4.12. [YAML](#yaml)\n  * 4.13. [TBLN](#tbln)\n  * 4.14. [WIDTH](#width)\n  * 4.15. [TEXT](#text)\n  * 4.16. [Raw output](#raw-output)\n  * 4.17. [ASCII Table \u0026 MarkDown output](#ascii-table-\u0026-markdown-output)\n  * 4.18. [Vertical format output](#vertical-format-output)\n* 5. [SQL](#sql)\n  * 5.1. [SQL function](#sql-function)\n  * 5.2. [JOIN](#join)\n  * 5.3. [PostgreSQL](#postgresql)\n    * 5.3.1. [Function](#function)\n    * 5.3.2. [Join table and CSV file is possible](#join-table-and-csv-file-is-possible)\n  * 5.4. [MySQL](#mysql)\n  * 5.5. [Analyze](#analyze)\n  * 5.6. [Configuration](#configuration)\n* 6. [Library](#library)\n* 7. [See also](#see-also)\n* 8. [Learn More](#learn-more)\n* 9. [License](#license)\n\n\u003c!-- vscode-markdown-toc-config\n\tnumbering=true\n\tautoSave=true\n\t/vscode-markdown-toc-config --\u003e\n\u003c!-- /vscode-markdown-toc --\u003e\n\n##  1. \u003ca name='install'\u003e\u003c/a\u003eINSTALL\n\n###  1.1. \u003ca name='go-get'\u003e\u003c/a\u003ego get\n\n```console\ngo get -d github.com/noborus/trdsql\ncd $GOPATH/src/github.com/noborus/trdsql\nmake\nmake install\n```\n\n####  1.1.1. \u003ca name='requirements'\u003e\u003c/a\u003eRequirements\n\ngo 1.21 or higher.\n\n###  1.2. \u003ca name='download-binary'\u003e\u003c/a\u003eDownload binary\n\nDownload binary from the [releases](https://github.com/noborus/trdsql/releases) page(Linux/Windows/macOS).\n\n###  1.3. \u003ca name='homebrew'\u003e\u003c/a\u003eHomebrew\n\n```console\nbrew install noborus/tap/trdsql\n```\n\n###  1.4. \u003ca name='macports'\u003e\u003c/a\u003eMacPorts\n\n```console\nsudo port selfupdate\nsudo port install trdsql\n```\n\n###  1.5. \u003ca name='freebsd'\u003e\u003c/a\u003eFreeBSD\n\n[freshports](https://www.freshports.org/textproc/trdsql/)\n\n```console\npkg install trdsql\n```\n\n###  1.6. \u003ca name='cgo-free'\u003e\u003c/a\u003eCgo free\n\nTypically, [go-sqlite3](https://github.com/mattn/go-sqlite3) is used for building.\nHowever, if you're building with `CGO_ENABLED=0`, consider using [sqlite](https://gitlab.com/cznic/sqlite) instead.\n\nBuilding without CGO (CGO Free) can reduce issues related to cross-compiling, but it may result in slower execution times.\n\n##  2. \u003ca name='docker'\u003e\u003c/a\u003eDocker\n\n###  2.1. \u003ca name='docker-pull'\u003e\u003c/a\u003eDocker pull\n\nPull the latest image from the Docker hub.\n\n```console\ndocker pull noborus/trdsql\n```\n\n###  2.2. \u003ca name='image-build'\u003e\u003c/a\u003eimage build\n\nOr build it yourself.\n\n```console\ndocker build -t trdsql .\n```\n\n###  2.3. \u003ca name='docker-run'\u003e\u003c/a\u003eDocker Run\n\nDocker run.\n\n```console\ndocker run --rm -it -v $(pwd):/tmp trdsql [options and commands]\n```\n\n##  3. \u003ca name='usage'\u003e\u003c/a\u003eUsage\n\nTo use trdsql, you can either specify an SQL query or simply provide a file for conversion.\n\n```console\ntrdsql [options] SQL\n```\n\nFor file conversion, this is equivalent to executing 'trdsql -o[output format] \"SELECT * FROM filename\"'.\n\n```console\ntrdsql -o[output format] -t [input filename]\n```\n\n###  3.1. \u003ca name='global-options'\u003e\u003c/a\u003eGlobal options\n\n* `-a` **filename** analyze the file and suggest SQL.\n* `-A` **filename** analyze the file but only suggest SQL.\n* `-config` **filename** configuration file location.\n* `-db` **db name** specify db name of the setting.\n* `-dblist` display db list of configure.\n* `-driver` **string** database driver.  [ mysql | postgres | sqlite3 | sqlite(CGO Free) ] (default \"sqlite3\")\n* `-dsn` **string** database driver specific data source name.\n* `-debug` debug print.\n* `-help` display usage information.\n* `-q` **filename** read query from the specified file.\n* `-t` **filename** read table name from the specified file.\n* `-version` display version information.\n\n###  3.2. \u003ca name='input-formats'\u003e\u003c/a\u003eInput formats\n\n* `-ig` guess format from extension. (default)\n* `-icsv` CSV format for input.\n* `-ijson` JSON format for input.\n* `-iltsv` LTSV format for input.\n* `-iyaml` YAML format for input.\n* `-itbln` TBLN format for input.\n* `-iwidth` width specification format for input.\n* `-itext` text format for input.\n\n####  3.2.1. \u003ca name='input-options'\u003e\u003c/a\u003eInput options\n\n* `-ih` the first line is interpreted as column names(CSV only).\n* `-id` **character** field delimiter for input(default \",\")(CSV only).\n* `-ijq` **string** jq expression string for input(JSON/JSONL only).\n* `-ilr` **int** limited number of rows to read.\n* `-inull` **string** value(string) to convert to null on input.\n* `-inum` add row number column.\n* `-ir` **int** number of rows to preread. (default 1)\n* `-is` **int** skip header row.\n\n###  3.3. \u003ca name='output-formats'\u003e\u003c/a\u003eOutput formats\n\n* `-ocsv` CSV format for output. (default)\n* `-ojson` JSON format for output.\n* `-ojsonl` JSONL(JSON Lines) format for output.\n* `-oltsv` LTSV format for output.\n* `-oat` ASCII Table format for output.\n* `-omd` Markdown format for output.\n* `-oraw` Raw format for output.\n* `-ovf` Vertical format for output.\n* `-oyaml` YAML format for output.\n* `-otbln` TBLN format for output.\n\nOr, [guess the output format by file name](#Guessbyoutputfilename).\n\n####  3.3.1. \u003ca name='output-options'\u003e\u003c/a\u003eOutput options\n\n* `-out` **filename** output file name.\n* `-out-without-guess` output without guessing (when using -out).\n* `-oh` output column name as header.\n* `-od` **character** field delimiter for output. (default \",\")(CSV and RAW only).\n* `-oq` **character** quote character for output. (default \"\\\"\")(CSV only).\n* `-oaq` enclose all fields in quotes for output(CSV only).\n* `-ocrlf` use CRLF for output. End each output line with '\\\\r\\\\n' instead of '\\\\n'.\"(CSV only).\n* `-onowrap` do not wrap long columns(AT and MD only).\n* `-onull` value(string) to convert from null on output.\n* `-oz` **string** compression format for output. [ gzip | bz2 | zstd | lz4 | xz ]\n\n###  3.4. \u003ca name='handling-of-null'\u003e\u003c/a\u003eHandling of NULL\n\nNULL is undecided in many text formats.\nJSON `null` is considered the same as SQL `NULL`.\nFor formats other than JSON, you must specify a string that is considered NULL.\nIn most cases you will need to specify an empty string (\"\").\n\nIf `-inull \"\"` is specified, an empty string will be treated as SQL NULL.\n\nSQL NULL is an empty string by default. Specify the -onull \"(NULL)\" option if you want a different string.\n\n```console\n$ echo \"1,,v\" | trdsql -inull \"\" -onull \"(NULL)\" \"SELECT * FROM -\"\n1,(NULL),v\n```\n\nIn the case of JSON, null is NULL as it is, and the specified string is converted to NULL.\n\n```console\n$ echo '[1,null,\"\"]' | trdsql -inull \"\" -ojson -ijson \"SELECT * FROM -\"\n[\n  {\n    \"c1\": \"1\"\n  },\n  {\n    \"c1\": null\n  },\n  {\n    \"c1\": null\n  }\n]\n```\n\n###  3.5. \u003ca name='multiple-queries'\u003e\u003c/a\u003eMultiple queries\n\nMultiple queries can be executed by separating them with a semicolon.\nUpdate queries must be followed by a SELECT statement.\n\n```console\n$ trdsql \"UPDATE SET c2='banana' WHERE c3='1';SELECT * FROM test.csv\"\n1,Orange\n2,Melon\n3,banana\n```\n\nYou can perform multiple SELECTs, but the output will be in one format.\n\n```console\n$ trdsql -oh \"SELECT c1,c2 FROM test.csv;SELECT c2,c1 FROM test.csv\"\nc1,c2\n1,Orange\n2,Melon\n3,Apple\nc2,c1\nOrange,1\nMelon,2\nApple,3\n```\n\n##  4. \u003ca name='example'\u003e\u003c/a\u003eExample\n\ntest.csv file.\n\n```csv\n1,Orange\n2,Melon\n3,Apple\n```\n\nPlease write a file name like a table name.\n\n```console\ntrdsql \"SELECT * FROM test.csv\"\n```\n\n`-q filename` can execute SQL from file\n\n```console\ntrdsql -q test.sql\n```\n\n###  4.1. \u003ca name='stdin-input'\u003e\u003c/a\u003eSTDIN input\n\n\"`-`\" or \"`stdin`\" is received from standard input instead of file name.\n\n```console\ncat test.csv | trdsql \"SELECT * FROM -\"\n```\n\nor\n\n```console\ncat test.csv | trdsql \"SELECT * FROM stdin\"\n```\n\n###  4.2. \u003ca name='multiple-files'\u003e\u003c/a\u003eMultiple files\n\nMultiple matched files can be executed as one table.\n\n```console\n$ trdsql -ih \"SELECT * FROM tt*.csv\"\n1,test1\n2,test2\n3,test3\n```\n\n\u003e [!NOTE]\n\u003e It is not possible to mix different formats (ex: CSV and LTSV).\n\n###  4.3. \u003ca name='compressed-files'\u003e\u003c/a\u003eCompressed files\n\nIf the file is compressed with gzip, bz2, zstd, lz4, xz, it will be automatically uncompressed.\n\n```console\ntrdsql \"SELECT * FROM testdata/test.csv.gz\"\n```\n\n```console\ntrdsql \"SELECT * FROM testdata/test.csv.zst\"\n```\n\nIt is possible to mix uncompressed and compressed files using wildcards.\n\n```console\ntrdsql \"SELECT * FROM testdata/test.csv*\"\n```\n\n###  4.4. \u003ca name='output-file'\u003e\u003c/a\u003eOutput file\n\n`-out filename` option to output the file to a file.\n\n```console\ntrdsql -out result.csv \"SELECT * FROM testdata/test.csv ORDER BY c1\"\n```\n\n###  4.5. \u003ca name='output-compression'\u003e\u003c/a\u003eOutput compression\n\n`-oz compression type` to compress and output.\n\n```console\ntrdsql -oz gz \"SELECT * FROM testdata/test.csv ORDER BY c1\" \u003e result.csv.gz\n```\n\n###  4.6. \u003ca name='guess-by-output-file-name'\u003e\u003c/a\u003eGuess by output file name\n\nThe filename of `-out filename` option determines\nthe output format(csv, ltsv, json, tbln, raw, md, at, vf, jsonl)\n and compression format(gzip, bz2, zstd,lz4, xz) by guess.\n\nGuess by extension output format + output compression\n(eg .csv.gz, .ltsv.lz4, .md.zst ...).\n\nThe following is an LTSV file compressed in zstd.\n\n```console\ntrdsql -out result.ltsv.zst \"SELECT * FROM testdata/test.csv\"\n```\n\n###  4.7. \u003ca name='columns-is-not-constant'\u003e\u003c/a\u003eColumns is not constant\n\nIf the number of columns is not a constant, read and decide multiple rows.\n\n```console\n$ trdsql -ir 3 -iltsv \"SELECT * FROM test_indefinite.ltsv\"\n1,Orange,50,,\n2,Melon,500,ibaraki,\n3,Apple,100,aomori,red\n```\n\n###  4.8. \u003ca name='tsv-(tab-separated-value)'\u003e\u003c/a\u003eTSV (Tab Separated Value)\n\n`-id \"\\\\t\"` is input from TSV (Tab Separated Value)\n\n```tsv\n1\tOrange\n2\tMelon\n3\tApple\n```\n\n```console\ntrdsql -id \"\\t\" \"SELECT * FROM test-tab.csv\"\n```\n\n`-od \"\\\\t\"` is TSV (Tab Separated Value) output.\n\n```console\n$ trdsql -od \"\\t\" \"SELECT * FROM test.csv\"\n1\tOrange\n2\tMelon\n3\tApple\n```\n\n###  4.9. \u003ca name='ltsv-(labeled-tab-separated-values)'\u003e\u003c/a\u003eLTSV (Labeled Tab-separated Values)\n\n`-iltsv` is input from LTSV(Labeled Tab-separated Values).\n\nsample.ltsv\n\n```ltsv\nid:1\tname:Orange\tprice:50\nid:2\tname:Melon\tprice:500\nid:3\tname:Apple\tprice:100\n```\n\n```console\ntrdsql -iltsv \"SELECT * FROM sample.ltsv\"\n```\n\n```ltsv\n1,Orange,50\n2,Melon,500\n3,Apple,100\n```\n\n\u003e [!NOTE]\n\u003e Only the columns in the first row are targeted.\n\n`-oltsv` is LTSV(Labeled Tab-separated Values) output.\n\n```console\n$ trdsql -iltsv -oltsv \"SELECT * FROM sample.ltsv\"\nid:1\tname:Orange\tprice:50\nid:2\tname:Melon\tprice:500\nid:3\tname:Apple\tprice:100\n```\n\n###  4.10. \u003ca name='json'\u003e\u003c/a\u003eJSON\n\n`-ijson` is input from JSON.\n\nsample.json\n\n```json\n[\n  {\n    \"id\": \"1\",\n    \"name\": \"Orange\",\n    \"price\": \"50\"\n  },\n  {\n    \"id\": \"2\",\n    \"name\": \"Melon\",\n    \"price\": \"500\"\n  },\n  {\n    \"id\": \"3\",\n    \"name\": \"Apple\",\n    \"price\": \"100\"\n  }\n]\n```\n\n```console\n$ trdsql -ijson \"SELECT * FROM sample.json\"\n1,Orange,50\n2,Melon,500\n3,Apple,100\n```\n\nJSON can contain structured types, but trdsql is stored as it is as JSON string.\n\nsample2.json\n\n```json\n[\n    {\n      \"id\": 1,\n      \"name\": \"Drolet\",\n      \"attribute\": { \"country\": \"Maldives\", \"color\": \"burlywood\" }\n    },\n    {\n      \"id\": 2,\n      \"name\": \"Shelly\",\n      \"attribute\": { \"country\": \"Yemen\", \"color\": \"plum\" }\n    },\n    {\n      \"id\": 3,\n      \"name\": \"Tuck\",\n      \"attribute\": { \"country\": \"Mayotte\", \"color\": \"antiquewhite\" }\n    }\n]\n```\n\n```console\n$ trdsql -ijson \"SELECT * FROM sample2.json\"\n1,Drolet,\"{\"\"color\"\":\"\"burlywood\"\",\"\"country\"\":\"\"Maldives\"\"}\"\n2,Shelly,\"{\"\"color\"\":\"\"plum\"\",\"\"country\"\":\"\"Yemen\"\"}\"\n3,Tuck,\"{\"\"color\"\":\"\"antiquewhite\"\",\"\"country\"\":\"\"Mayotte\"\"}\"\n```\n\nPlease use SQL function.\n\n* [SQLite3 - JSON Functions And Operators](https://www.sqlite.org/json1.html)\n* [PostgreSQL - JSON Functions and Operators](https://www.postgresql.org/docs/current/functions-json.html)\n* [MySQL - Functions That Search JSON Values](https://dev.mysql.com/doc/refman/8.0/en/json-search-functions.html)\n\n```console\n$ trdsql -ijson \"SELECT id, name, JSON_EXTRACT(attribute,'$.country'), JSON_EXTRACT(attribute,'$.color') FROM sample2.json\"\n1,Drolet,Maldives,burlywood\n2,Shelly,Yemen,plum\n3,Tuck,Mayotte,antiquewhite\n```\n\n####  4.10.1. \u003ca name='jq-expression'\u003e\u003c/a\u003ejq expression\n\nIf json has a hierarchy, you can filter by [jq](https://stedolan.github.io/jq/) expression.\n\nThe jq expression is implemented using [gojq](https://github.com/itchyny/gojq).\n\nmenu.json\n\n```json\n{\n\t\"menu\": {\n\t\t\"id\": \"file\",\n\t\t\"value\": \"File\",\n\t\t\"popup\": {\n\t\t\t\"menuitem\": [\n\t\t\t\t{\n\t\t\t\t\t\"value\": \"New\",\n\t\t\t\t\t\"onclick\": \"CreateDoc()\"\n\t\t\t\t},\n\t\t\t\t{\n\t\t\t\t\t\"value\": \"Open\",\n\t\t\t\t\t\"onclick\": \"OpenDoc()\"\n\t\t\t\t},\n\t\t\t\t{\n\t\t\t\t\t\"value\": \"Save\",\n\t\t\t\t\t\"onclick\": \"SaveDoc()\"\n\t\t\t\t}\n\t\t\t]\n\t\t}\n\t}\n}\n```\n\nYou can write a jq expression by connecting :: after the json file name.\nEnclose the jq expression in double quotes if needed.\n\n```console\ntrdsql -oat 'SELECT value, onclick FROM menu.json::\".menu.popup.menuitem\"'\n```\n\nOr specify with the `-ijq` option.\n\n```console\n$ trdsql -oat -ijq \".menu.popup.menuitem\" \"SELECT * FROM menu.json\"\n+-------+-------------+\n| value |   onclick   |\n+-------+-------------+\n| New   | CreateDoc() |\n| Open  | OpenDoc()   |\n| Save  | SaveDoc()   |\n+-------+-------------+\n```\n\nExample to use instead of [gojq](https://github.com/itchyny/gojq#usage).\n\n```sh\n$ echo '{\"foo\": 128}' | trdsql -ijson \"SELECT * FROM -::'.foo'\"\n128\n$ echo '{\"a\": {\"b\": 42}}' | trdsql -ijson \"SELECT * FROM -::'.a.b'\"\n42\n$ echo '{\"id\": \"sample\", \"10\": {\"b\": 42}}' | trdsql -ijson \"SELECT * FROM -::'{(.id): .[\\\"10\\\"].b}'\"\n42\n$ echo '[{\"id\":1},{\"id\":2},{\"id\":3}]' | trdsql -ijson \"SELECT * FROM -::'.[] | .id'\"\n1\n2\n3\n$ echo '{\"a\":1,\"b\":2}' | trdsql -ijson \"SELECT * FROM -::'.a += 1 | .b *= 2'\"\n4,2\n$ echo '{\"a\":1} [2] 3' | trdsql -ijson \"SELECT * FROM -::'. as {\\$a} ?// [\\$a] ?// \\$a | \\$a'\"\n1\n2\n3\n```\n\n###  4.11. \u003ca name='jsonl(ndjson)'\u003e\u003c/a\u003eJSONL(NDJSON)\n\nAnother json format. JSONL(JSON Lines). It is also called [ndjson](http://ndjson.org/).\n\nsample2.json\n\n```json\n{\"id\": \"1\",\"name\": \"Orange\",\"price\": \"50\"}\n{\"id\": \"2\",\"name\": \"Melon\",\"price\": \"500\"}\n{\"id\": \"3\",\"name\": \"Apple\",\"price\": \"100\"}\n```\n\n`-ojson` is JSON Output.\n\n```console\n$ trdsql -ojson \"SELECT * FROM test.csv\"\n[\n  {\n    \"c1\": \"1\",\n    \"c2\": \"Orange\"\n  },\n  {\n    \"c1\": \"2\",\n    \"c2\": \"Melon\"\n  },\n  {\n    \"c1\": \"3\",\n    \"c2\": \"Apple\"\n  }\n]\n```\n\nTo output in JSONL, specify `-ojsonl`.\n\n```console\n$ trdsql -ojsonl \"SELECT * FROM test.csv\"\n{\"c1\":\"1\",\"c2\":\"Orange\"}\n{\"c1\":\"2\",\"c2\":\"Melon\"}\n{\"c1\":\"3\",\"c2\":\"Apple\"}\n```\n\n###  4.12. \u003ca name='yaml'\u003e\u003c/a\u003eYAML\n\n`-iyaml` is input from YAML\n(Or if the extension is `yaml` or `yml`, it is considered a YAML file).\n\nsample.yaml\n\n```yaml\n- id: 1\n  name: Orange\n  price: 50\n- id: 2\n  name: Melon\n  price: 500\n- id: 3\n  name: Apple\n  price: 100\n```\n\n```console\n$ trdsql -iyaml -ocsv \"SELECT * FROM sample.yaml\"\n1,Orange,50\n2,Melon,500\n3,Apple,100\n```\n\nSince yaml is internally converted to JSON, it can be converted to json and output.\n\nsample2.yaml\n\n```yaml\na: true\nb:\n  c: 2\n  d: [3, 4, 5]\n  e:\n    - name: fred\n      value: 3\n    - name: sam\n      value: 4%\n```\n\n```console\n$ trdsql -ojson \"SELECT * FROM sample2.yaml\"\n[\n  {\n    \"a\": \"true\",\n    \"b\": {\n      \"c\": 2,\n      \"d\": [\n        3,\n        4,\n        5\n      ],\n      \"e\": [\n        {\n          \"name\": \"fred\",\n          \"value\": 3\n        },\n        {\n          \"name\": \"sam\",\n          \"value\": \"4%\"\n        }\n      ]\n    }\n  }\n]\n```\n\nSo in addition you can also use `jq` syntax.\n\n```console\n$ trdsql  -ojson \"SELECT * FROM sample2.yaml::.b.e\"\n[\n  {\n    \"name\": \"fred\",\n    \"value\": \"3\"\n  },\n  {\n    \"name\": \"sam\",\n    \"value\": \"4%\"\n  }\n]\n```\n\njson can be converted to yaml.\n\n```console\n$ trdsql -ojson \"SELECT * FROM sample2.yaml::.b.e\"\n- name: fred\n  value: 3\n- name: sam\n  value: 4%\n```\n\n###  4.13. \u003ca name='tbln'\u003e\u003c/a\u003eTBLN\n\n`-itbln` is input from TBLN.\n\nsample.tbln\n\n```TBLN\n; name: | id | name |\n; type: | int | text |\n| 1 | Bob |\n| 2 | Alice |\n```\n\n```console\n$ trdsql -itbln \"SELECT * FROM sample.tbln\"\n1,Bob\n2,Alice\n```\n\nTBLN file reflects extras name and type.\n\n`-otbln` is TBLN Output.\n\n```console\n$ trdsql -otbln \"SELECT c1::int as id, c2::text as name FROM test.csv\"\n; created_at: 2019-03-22T13:20:31+09:00\n; name: | id | name |\n; type: | int | text |\n| 1 | Orange |\n| 2 | Melon |\n| 3 | Apple |\n```\n\nTBLN can contain column names and type definitions.\nPlease refer to \u003chttps://tbln.dev/\u003e for details of TBLN.\n\n###  4.14. \u003ca name='width'\u003e\u003c/a\u003eWIDTH\n\n`-iwidth` inputs the format specifying the width.\nThis is used when the header column width represents the body column width.\n\n```console\n$ ps | trdsql -oh -iwidth \"SELECT * FROM -\"\nPID,TTY,TIME,CMD\n302965,pts/3,00:00:12,zsh\n733211,pts/3,00:00:00,ps\n733212,pts/3,00:00:00,tee\n733213,pts/3,00:00:00,guesswidth\n```\n\n`-id \" \"` for `CSV` also works in many cases.\nBut `-id \" \"` does not recognize spaces in columns very well.\n\n`-iwidth` recognizes column widths and space separators.\n\n###  4.15. \u003ca name='text'\u003e\u003c/a\u003eTEXT\n\nThe `-itext` option or files with “.text”extension are in text format.\n\nThis is a one line to one column format.\nA blank line is also a line, unlike the `CSV` format.\n\n```console\n$ cat test.text\na\n\nb\n\nc\n$ trdsql -itext \"SELECT * FROM test.text\"\na\n\nb\n\nc\n```\n\nIt is useful in conjunction with the `-inum` option.\n\n```console\n$ trdsql -inum \"SELECT * FROM test.text\"\n1,a\n2,\n3,b\n4,\n5,c\n```\n\n###  4.16. \u003ca name='raw-output'\u003e\u003c/a\u003eRaw output\n\n`-oraw` is Raw Output.\nIt is used when \"escape processing is unnecessary\" in CSV output.\n(For example, when outputting JSON in the database).\n\n```console\n$ trdsql -oraw \"SELECT row_to_json(t,TRUE) FROM test.csv AS t\"\n{\"c1\":\"1\",\n \"c2\":\"Orange\"}\n{\"c1\":\"2\",\n \"c2\":\"Melon\"}\n{\"c1\":\"3\",\n \"c2\":\"Apple\"}\n```\n\nMultiple delimiter characters can be used for raw.\n\n```console\n$ trdsql -oraw -od \"\\t|\\t\" -db pdb \"SELECT * FROM test.csv\"\n1\t|\tOrange\n2\t|\tMelon\n3\t|\tApple\n```\n\n###  4.17. \u003ca name='ascii-table-\u0026-markdown-output'\u003e\u003c/a\u003eASCII Table \u0026 MarkDown output\n\n`-oat` is ASCII table output.\n\n```console\n$ trdsql -oat \"SELECT * FROM test.csv\"\n+----+--------+\n| C1 |   C2   |\n+----+--------+\n|  1 | Orange |\n|  2 | Melon  |\n|  3 | Apple  |\n+----+--------+\n```\n\n`-omd` is Markdown output.\n\n```console\n$ trdsql -omd \"SELECT * FROM test.csv\"\n| C1 |   C2   |\n|----|--------|\n|  1 | Orange |\n|  2 | Melon  |\n|  3 | Apple  |\n```\n\nThe `-onowrap` option does not wrap long columns in `at` or `md` output.\n\n###  4.18. \u003ca name='vertical-format-output'\u003e\u003c/a\u003eVertical format output\n\n`-ovf` is Vertical format output(\"column name | value\" vertically).\n\n```console\n$ trdsql -ovf \"SELECT * FROM test.csv\"\n---[ 1]--------------------------------------------------------\n  c1 | 1\n  c2 | Orange\n---[ 2]--------------------------------------------------------\n  c1 | 2\n  c2 | Melon\n---[ 3]--------------------------------------------------------\n  c1 | 3\n  c2 | Apple\n```\n\n##  5. \u003ca name='sql'\u003e\u003c/a\u003eSQL\n\n###  5.1. \u003ca name='sql-function'\u003e\u003c/a\u003eSQL function\n\n```console\n$ trdsql \"SELECT count(*) FROM test.csv\"\n3\n```\n\nThe default column names are c1, c2,...\n\n```console\n$ trdsql \"SELECT c2,c1 FROM test.csv\"\nOrange,1\nMelon,2\nApple,3\n```\n\n\u003e [!NOTE]\n\u003e the available functions and their syntax depend on the driver you have chosen (mysql or postgres or sqlite). The default one is sqlite.\n\n###  5.2. \u003ca name='join'\u003e\u003c/a\u003eJOIN\n\nThe SQL JOIN can be used.\n\nuser.csv\n\n```csv\n1,userA\n2,userB\n```\n\nhist.csv\n\n```csv\n1,2017-7-10\n2,2017-7-10\n2,2017-7-11\n```\n\n```console\n$ trdsql \"SELECT u.c1,u.c2,h.c2 FROM user.csv as u LEFT JOIN hist.csv as h ON(u.c1=h.c1)\"\n1,userA,2017-7-10\n2,userB,2017-7-10\n2,userB,2017-7-11\n```\n\n###  5.3. \u003ca name='postgresql'\u003e\u003c/a\u003ePostgreSQL\n\nWhen using PostgreSQL, specify postgres for driver\n and driver-specific data source name for dsn.\n\n```console\ntrdsql -driver postgres -dsn \"dbname=test\" \"SELECT count(*) FROM test.csv \"\n```\n\n####  5.3.1. \u003ca name='function'\u003e\u003c/a\u003eFunction\n\nThe PostgreSQL driver can use the window function.\n\n```console\n$ trdsql -driver postgres -dsn \"dbname=test\" \"SELECT row_number() OVER (ORDER BY c2),c1,c2 FROM test.csv\"\n1,3,Apple\n2,2,Melon\n3,1,Orange\n```\n\nFor example, the generate_series function can be used.\n\n```console\n$ trdsql -driver postgres -dsn \"dbname=test\" \"SELECT generate_series(1,3);\"\n1\n2\n3\n```\n\n####  5.3.2. \u003ca name='join-table-and-csv-file-is-possible'\u003e\u003c/a\u003eJoin table and CSV file is possible\n\nTest database has a colors table.\n\n```console\n$ psql test -c \"SELECT * FROM colors\"\n id |  name  \n----+--------\n  1 | orange\n  2 | green\n  3 | red\n(3 rows)\n```\n\nJoin table and CSV file.\n\n```console\n$ trdsql -driver postgres -dsn \"dbname=test\" \"SELECT t.c1,t.c2,c.name FROM test.csv AS t LEFT JOIN colors AS c ON (t.c1::int = c.id)\"\n1,Orange,orange\n2,Melon,green\n3,Apple,red\n```\n\nTo create a table from a file, use \"CREATE TABLE ... AS SELECT...\".\n\n```console\ntrdsql -driver postgres -dns \"dbname=test\" \"CREATE TABLE fruits (id, name) AS SELECT c1::int, c2 FROM fruits.csv \"\n```\n\n```console\n$ psql -c \"SELECT * FROM fruits;\"\n id |  name  \n----+--------\n  1 | Orange\n  2 | Melon\n  3 | Apple\n(3 rows)\n```\n\n###  5.4. \u003ca name='mysql'\u003e\u003c/a\u003eMySQL\n\nWhen using MySQL, specify mysql for driver and connection information for dsn.\n\n```console\n$ trdsql -driver mysql -dsn \"user:password@/test\" \"SELECT GROUP_CONCAT(c2 ORDER BY c2 DESC) FROM testdata/test.csv\"\n\"g,d,a\"\n```\n\n```console\n$ trdsql -driver mysql -dsn \"user:password@/test\" \"SELECT c1, SHA2(c2,224) FROM test.csv\"\n1,a063876767f00792bac16d0dac57457fc88863709361a1bb33f13dfb\n2,2e7906d37e9523efeefb6fd2bc3be6b3f2991678427bedc296f9ddb6\n3,d0b8d1d417a45c7c58202f55cbb617865f1ef72c606f9bce54322802\n```\n\nMySQL can join tables and CSV files as well as PostgreSQL.\n\n###  5.5. \u003ca name='analyze'\u003e\u003c/a\u003eAnalyze\n\nThe ***-a filename*** option parses the file and outputs table information and SQL examples.\n\n```console\n$ trdsql -a testdata/test.ltsv\nThe table name is testdata/header.csv.\nThe file type is CSV.\n\nData types:\n+-------------+------+\n| column name | type |\n+-------------+------+\n| id          | text |\n| \\`name\\`    | text |\n+-------------+------+\n\nData samples:\n+----+----------+\n| id | \\`name\\` |\n+----+----------+\n|  1 | Orange   |\n+----+----------+\n\nExamples:\ntrdsql -db sdb -ih \"SELECT id, \\`name\\` FROM testdata/header.csv\"\ntrdsql -db sdb -ih \"SELECT id, \\`name\\` FROM testdata/header.csv WHERE id = '1'\"\ntrdsql -db sdb -ih \"SELECT id, count(id) FROM testdata/header.csv GROUP BY id\"\ntrdsql -db sdb -ih \"SELECT id, \\`name\\` FROM testdata/header.csv ORDER BY id LIMIT 10\"\n```\n\nOther options(-id,-ih,-ir,-is,icsv,iltsv,-ijson,-itbln...) are available.\n\n```console\ntrdsql -ih -a testdata/header.csv\n```\n\nSimilarly, with  ***-A filename*** option, only Examples (SQL) is output.\n\n```console\n$ trdsql -ih -A testdata/header.csv\ntrdsql -ih \"SELECT id, \\`name\\` FROM testdata/header.csv\"\ntrdsql -ih \"SELECT id, \\`name\\` FROM testdata/header.csv WHERE id = '1'\"\ntrdsql -ih \"SELECT id, count(id) FROM testdata/header.csv GROUP BY id\"\ntrdsql -ih \"SELECT id, \\`name\\` FROM testdata/header.csv ORDER BY id LIMIT 10\"\n```\n\n###  5.6. \u003ca name='configuration'\u003e\u003c/a\u003eConfiguration\n\nYou can specify driver and dsn in the configuration file.\n\nUnix like.\n\n```path\n${HOME}/.config/trdsql/config.json\n```\n\nWindows (ex).\n\n```path\nC:\\Users\\{\"User\"}\\AppData\\Roaming\\trdsql\\config.json\n```\n\nOr use the -config file option.\n\n```console\ntrdsql -config config.json \"SELECT * FROM test.csv\"\n```\n\n sample: [config.json](config.json.sample)\n\n```json\n{\n  \"db\": \"pdb\",\n  \"database\": {\n    \"sdb\": {\n      \"driver\": \"sqlite3\",\n      \"dsn\": \"\"\n    },\n    \"pdb\": {\n      \"driver\": \"postgres\",\n      \"dsn\": \"user=test dbname=test\"\n    },\n    \"mdb\": {\n      \"driver\": \"mysql\",\n      \"dsn\": \"user:password@/dbname\"\n    }\n  }\n}\n```\n\nThe default database is an entry of \"db\".\n\nIf you put the setting in you can specify the name with -db.\n\n```console\n$ trdsql -debug -db pdb \"SELECT * FROM test.csv\"\n2017/07/18 02:27:47 driver: postgres, dsn: user=test dbname=test\n2017/07/18 02:27:47 CREATE TEMPORARY TABLE \"test.csv\" ( c1 text,c2 text );\n2017/07/18 02:27:47 INSERT INTO \"test.csv\" (c1,c2) VALUES ($1,$2);\n2017/07/18 02:27:47 SELECT * FROM \"test.csv\"\n1,Orange\n2,Melon\n3,Apple\n```\n\n##  6. \u003ca name='library'\u003e\u003c/a\u003eLibrary\n\nExample of use as a library.\n\n```go\npackage main\n\nimport (\n        \"log\"\n\n        \"github.com/noborus/trdsql\"\n)\n\nfunc main() {\n        trd := trdsql.NewTRDSQL(\n                trdsql.NewImporter(trdsql.InDelimiter(\":\")),\n                trdsql.NewExporter(trdsql.NewWriter()),\n        )\n        if err := trd.Exec(\"SELECT c1 FROM /etc/passwd\"); err != nil {\n                log.Fatal(err)\n        }\n}\n```\n\nPlease refer to [godoc](https://pkg.go.dev/github.com/noborus/trdsql) and _example for usage as a library.\n\n##  7. \u003ca name='see-also'\u003e\u003c/a\u003eSee also\n\n* [psutilsql](https://github.com/noborus/psutilsql) - A tool for querying system status in SQL.\n* [mdtsql](https://github.com/noborus/mdtsql) - A tool for querying markdown tables in SQL.\n* [xlsxsql](https://github.com/noborus/xlsxsql) - A tool for querying Excel files in SQL.\n\n##  8. \u003ca name='learn-more'\u003e\u003c/a\u003eLearn More\n\n* https://noborus.github.io/trdsql/index.html\n* https://cn.x-cmd.com/pkg/trdsql#trdsql\n* https://x-cmd.com/1min/trdsql#trdsql\n\n##  9. \u003ca name='license'\u003e\u003c/a\u003eLicense\n\nMIT\n\nPlease check each license of SQL driver.\n\n* [SQLite](https://github.com/mattn/go-sqlite3)\n* [MySQL](https://github.com/go-sql-driver/mysql)\n* [PostgreSQL](https://github.com/lib/pq)\n","funding_links":["https://github.com/sponsors/noborus"],"categories":["开源类库","Go","\u003ca name=\"data\"\u003e\u003c/a\u003edata","cli","Repositories","SQL"],"sub_categories":["终端工具","Über SQL"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnoborus%2Ftrdsql","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fnoborus%2Ftrdsql","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnoborus%2Ftrdsql/lists"}