github.com/mithrandie/csvq@v1.18.1/docs/_posts/2006-01-02-command.md (about) 1 --- 2 layout: default 3 title: Command Usage - Reference Manual - csvq 4 category: reference 5 --- 6 7 # Command Usage 8 9 ``` 10 csvq [options] [subcommand] ["query"|argument] 11 ``` 12 13 * [Execution of Statements](#execution) 14 * [Options](#options) 15 * [Subcommands](#subcommands) 16 * [Configurations](#configurations) 17 * [Special Characters](#special_characters) 18 * [Return Code](#return_code) 19 20 ## Execution of Statements 21 {: #execution} 22 23 There are three ways to execute a query or statements. 24 25 ```bash 26 # Pass to the csvq command as an argument 27 $ csvq 'SELECT id, name FROM users' 28 29 # Load from file 30 $ cat statements.sql 31 VAR @id := 0; 32 SELECT @id := @id + 1 AS id, 33 name 34 FROM users; 35 36 $ csvq -s statements.sql 37 38 # Execute in the interactive shell 39 $ csvq 40 csvq > SELECT id, name FROM users; 41 +----+-------+ 42 | id | name | 43 +----+-------+ 44 | 1 | Louis | 45 | 2 | Sean | 46 +----+-------+ 47 csvq > UPDATE users SET name = 'Mildred' WHERE id = 2; 48 1 record updated on "/home/mithrandie/docs/csv/users.csv". 49 csvq > SELECT id, name FROM users; 50 +----+----------+ 51 | id | name | 52 +----+----------+ 53 | 1 | Louis | 54 | 2 | Mildred | 55 +----+----------+ 56 csvq > COMMIT; 57 Commit: file "/home/mithrandie/docs/csv/users.csv" is updated. 58 csvq > 59 ``` 60 61 62 ## Options 63 {: options} 64 65 --allow-uneven-fields 66 : Allow loading CSV files with uneven field length. 67 68 --ansi-quotes, -k 69 : Use double quotation mark (U+0022 `"`) as identifier enclosure. 70 71 --color, -c 72 : Use ANSI color escape sequences. 73 74 --count-diacritical-sign, -S 75 : Count diacritical signs as halfwidth. If not, then that characters are counted as zero-width. 76 77 --count-format-code, -A 78 : Count format characters and zero-width spaces as halfwidth. If not, then that characters are counted as zero-width. 79 80 --cpu, -p 81 : Hint for the number of cpu cores to be used. The default is the half of the number of cpu cores. 82 83 --datetime-format value, -t value 84 : Datetime Format to parse strings. 85 86 Format string is the same as the function [DATETIME_FORMAT]({{ '/reference/datetime-functions.html#datetime_format' | relative_url }}). 87 88 This option can be specified multiple formats using JSON array of strings. 89 90 --delimiter value, -d value 91 : Field delimiter for CSV. The default is a comma(U+002C `,`). 92 93 A delimiter must be one character. [Special Characters](#special_characters) can be used with backslash escaping. 94 95 --delimiter-positions value, -m value 96 : Delimiter positions for Fixed-Length Format. The default is "SPACES". 97 98 You can specify "SPACES" or a JSON Array of integers. 99 "SPACES" splits lines automatically by spaces. 100 JSON Array is used to specify delimiter positions explicitly. 101 102 Delimiter positions indicate the number of bytes from the start of the line. 103 For example, JSON Array "[5, 10, 15]" splits "1234567890abcde" as "12345, 67890, abcde" 104 105 If you want to operate Single-Line Fixed-Length Format, then connect a JSON Array to "S"(U+0053) or "s"(U+0073). 106 For example, "S[2, 3, 6]" imports "01aabc02bdef03cghi" as "('01', 'a', 'abc'), ('02', 'b', 'def'), ('03', 'c', 'ghi')". 107 108 --east-asian-encoding, -W 109 : Count ambiguous characters as fullwidth. If not, then that characters are counted as halfwidth. 110 111 --enclose-all, -Q 112 : Enclose all string values in CSV. 113 114 --encoding value, -e value 115 : File encoding. Following encodings are supported. The default is _AUTO_. 116 117 | value(case ignored) | character encoding | 118 |:--------------------|:--------------------------------------------| 119 | AUTO | Detect file encoding automatically | 120 | UTF8 | UTF-8. Detect BOM automatically | 121 | UTF8M | UTF-8 with BOM | 122 | UTF16 | UTF-16. Detect BOM and Endian automatically | 123 | UTF16BE | UTF-16 Big-Endian | 124 | UTF16LE | UTF-16 Little-Endian | 125 | UTF16BEM | UTF-16 Big-Endian with BOM | 126 | UTF16LEM | UTF-16 Little-Endian with BOM | 127 | SJIS | Shift_JIS | 128 129 > JSON Format is supported only UTF-8. 130 131 > Whatever the value of this option is, if the first character in a file is a UTF-8 byte order mark, the file will be loaded as UTF-8 encoding. 132 133 --format value, -f value 134 : Format of query results. The default is _TEXT_, but _CSV_ is used for output to pipe. 135 136 | value(case ignored) | format | 137 |:--------------------|:-------------------------------------------------------------------| 138 | CSV | Character separated values. Separetor can be changed by -D option. | 139 | TSV | Tab separated values | 140 | FIXED | Fixed-Length Format | 141 | JSON | JSON | 142 | JSONL | JSON Lines | 143 | LTSV | Labeled Tab-separated Values | 144 | GFM | Text Table for GitHub Flavored Markdown | 145 | ORG | Text Table for Emacs Org-mode | 146 | BOX | Text Table using Box-drawing characters | 147 | TEXT | Text Table for console | 148 | JSONH | Alias of "--format JSON --json-escape HEX" | 149 | JSONA | Alias of "--format JSON --json-escape HEXALL" | 150 151 When this option is not specified, the file specified by the _--out_ option will be output in a specific format if it has the following extensions. 152 153 | file extension | format | 154 |:---------------|:-----------| 155 | .csv | CSV | 156 | .tsv | TSV | 157 | .json | JSON | 158 | .jsonl | JSON Lines | 159 | .ltsv | LTSV | 160 | .md | GFM | 161 | .org | ORG | 162 163 --import-format value, -i value 164 : Default format to load files. The default is _CSV_. 165 166 | value(case ignored) | format | 167 |:--------------------|:-------------------------------------------------------------------| 168 | CSV | Character separated values. Separetor can be changed by -D option. | 169 | TSV | Tab separated values | 170 | FIXED | Fixed-Length Format | 171 | JSON | JSON | 172 | JSONL | JSON Lines | 173 | LTSV | Labeled Tab-separated Values | 174 175 Regardless of this option, files with the following extensions will be read in a specific format. 176 177 | file extension | format | 178 |:---------------|:-----------------------------| 179 | .csv | CSV | 180 | .tsv | TSV | 181 | .json | JSON | 182 | .jsonl | JSON Lines | 183 | .ltsv | Labeled Tab-separated Values | 184 185 --json-escape, -J 186 : JSON escape type. The default is _BACKSLASH_. 187 188 | value(case ignored) | description | 189 |:--------------------|:-------------------------------------------------------| 190 | BACKSLASH | Escape special characters with backslashes(U+005C `\`) | 191 | HEX | Escape special characters with six-character sequence | 192 | HEXALL | Escape all strings with six-character sequence | 193 194 > [Escaped characters in JSON](#escaped_characters_in_json) 195 196 --json-query QUERY, -j QUERY 197 : [QUERY]({{ '/reference/json.html#query' | relative_url }}) for JSON. 198 199 --limit-recursion 200 : Maximum number of iterations for recursive queries. "-1" means no limit. The default is 1000. 201 202 --line-break value, -l value 203 : Line break in query results. One of following values. The default is _LF_. 204 205 | value(case ignored) | unicode character | 206 |:--------------------|:--------------------------------------------| 207 | CRLF | U+000D Carriage Return and U+000A Line Feed | 208 | CR | U+000D Carriage Return | 209 | LF | U+000A Line Feed | 210 211 --no-header, -n 212 : Import the first line as a record. 213 214 First line of a CSV file is dealt with as the header line. In case "--no-header" option passed, 215 fields are automatically named as "c" and following sequential number. e.g. "c1", "c2", "c3", ... 216 217 --out FILE, -o FILE 218 : Export result sets of select queries to FILE. 219 220 If the output file is not specified, the result sets are written to standard output. 221 222 --pretty-print, -P 223 : Make JSON output easier to read in query results. 224 225 --quiet, -q 226 : Suppress operation log output. 227 228 --repository PATH, -r PATH 229 : Directory Path where files are located. The default is the current directory. 230 231 --scientific-notation -SN 232 : Use Scientific Notation for large exponents in output. 233 234 --source FILE, -s FILE 235 : Load query or statements from FILE. 236 237 238 --stats, -x 239 : Show execution time and memory statistics. 240 241 Query Execution Time 242 : execution time of one query. select, insert, update, or delete queries are measured. 243 244 TotalTime 245 : total execution time 246 247 TotalAlloc 248 : cumulative bytes of allocated heap objects 249 250 HeapSys 251 : bytes of heap memory obtained from the OS 252 253 Mallocs 254 : cumulative count of heap objects allocated 255 256 Frees 257 : cumulative count of heap objects freed 258 259 --strict-equal, -g 260 : Compare strictly that two values are equal for DISTINCT, GROUP BY and ORDER BY. 261 262 --strip-ending-line-break, -T 263 : Strip line break from the end of files and query results. 264 265 --timezone value, -z value 266 : Default Timezone. The default is _Local_. 267 268 _Local_, _UTC_ or a timezone name in the IANA TimeZone database(in the form of _"Area/Location"_. e.g. _"America/Los_Angeles"_). 269 270 > The timezone database is required in order to use the timezone names. 271 > Most Unix-like systems provide the database. 272 > But if your system does not provide it and you have not installed Go Lang, then you must put the database file named [zoneinfo.zip](https://golang.org/lib/time/) to the directory "$ZONEINFO" or "$GOROOT/lib/time/". 273 274 --wait-timeout value, -w value 275 : Limit of the waiting time in seconds to wait for locked files to be released. The default is 10. 276 277 --without-header, -N 278 : Export result sets of select queries without the header line. 279 280 --without-null, -a 281 : Parse empty fields as empty strings. 282 283 In most cases CSV fields are imported as string values, but no-quoted empty fields are imported as nulls. 284 By using the "--without-null" option, no-quoted empty fields are imported as empty string values. 285 286 --write-delimiter value, -D value 287 : Field delimiter for query results in CSV format. The default is a comma(U+002C `,`). 288 289 --write-delimiter-positions value, -M value 290 : Delimiter positions for query results in Fixed-Length format. The default is "SPACES". 291 292 If the field value is shorter than the length of the field, the missing part is padded with SPACE(U+0020). 293 For example, JSON Array "[5, 10, 15]" combines "123, abc, def" into "␣␣123abc␣␣def␣␣". 294 295 --write-encoding value, -E value 296 : Character encoding of query results. The default is _UTF8_. 297 298 | value(case ignored) | character encoding | 299 |:--------------------|:------------------------------| 300 | UTF8 | UTF-8 | 301 | UTF8M | UTF-8 with BOM | 302 | UTF16 | An alias of UTF16BE | 303 | UTF16BE | UTF-16 Big-Endian | 304 | UTF16LE | UTF-16 Little-Endian | 305 | UTF16BEM | UTF-16 Big-Endian with BOM | 306 | UTF16LEM | UTF-16 Little-Endian with BOM | 307 | SJIS | Shift_JIS | 308 309 --help, -h 310 : Show help 311 312 --version, -v 313 : Print the version 314 315 > If you want to pass "false" to a boolean command option, you can specify it as "--option-name=false". 316 > Some command options can also be specified in statements by using [Set Flag Statements]({{ '/reference/flag.html' | relative_url }}). 317 318 ### Determination of file format 319 320 #### Loading 321 322 First, the file extension to be loaded is checked. If the extension is displayed in the following table, the associated file format will be selected. 323 Otherwise, the passed value by the "--import-format" option will be used to load. 324 325 | extension | file format | 326 |:----------|:------------| 327 | .csv | CSV | 328 | .tsv | TSV | 329 | .json | JSON | 330 | .jsonl | JSON Lines | 331 | .ltsv | LTSV | 332 333 The following options are available for loading. 334 335 - --delimiter value, -d value 336 - --delimiter-positions value, -m value 337 - --encoding value, -e value 338 - --json-query QUERY, -j QUERY 339 - --no-header, -n 340 - --without-null, -a 341 342 You can also use [Format Specified Functions]({{ '/reference/select-query.html#from_clause' | relative_url }}) to specify the format each file. 343 A format specified function effects the first loading in a transaction. 344 After the second loading, the specifications in the format specified function are ignored. 345 You must use the ROLLBACK statement to discard all changes in the transaction if you reload the same file. 346 347 #### Updating 348 349 The table attributes that were determined when loading will be used to updating. 350 You can modify table attributes by using [SET ATTRIBUTE Statement]({{ '/reference/alter-table-query.html#set-attribute' | relative_url }})). 351 352 #### Creating 353 354 First, the file extension to be loaded is checked. If the extension is displayed in the following table, the associated file format will be selected. 355 Otherwise, CSV is used as the format. 356 You can modify table attributes by using [SET ATTRIBUTE Statement]({{ '/reference/alter-table-query.html#set-attribute' | relative_url }})). 357 358 | extension | file format | 359 |:----------|:-------------------------| 360 | .csv | CSV | 361 | .tsv | TSV | 362 | .json | JSON | 363 | .jsonl | JSON Lines | 364 | .ltsv | LTSV | 365 | .md | GitHub Flavored Markdown | 366 | .org | Emacs Org-mode | 367 368 #### Exporting query results with the "--out" option 369 370 The passed value by the "--format" option will be used to export. 371 372 The following options are available for exporting. 373 374 - --count-diacritical-sign, -S 375 - --count-format-code, -A 376 - --east-asian-encoding, -W 377 - --enclose-all, -Q 378 - --json-escape, -J 379 - --line-break value, -l value 380 - --pretty-print, -P 381 - --without-header, -N 382 - --write-delimiter value, -D value 383 - --write-delimiter-positions value, -M value 384 - --write-encoding value, -E value 385 386 ## Subcommands 387 {: #subcommands} 388 389 | subcommand | description | 390 |:------------------------------|:---------------------------| 391 | [calc](#calc) | Calculate value from stdin | 392 | [check-update](#check-update) | Check for updates | 393 | [fields](#fields) | Show fields in file | 394 | [syntax](#syntax) | Print syntax | 395 | help, h | Shows help | 396 397 ### Calc Subcommand 398 {: #calc} 399 400 Calculate a value from stdin. 401 ```bash 402 csvq [options] calc "expression" 403 ``` 404 405 In the expression, the value from stdin is represented as "c1". 406 407 Example: 408 ```bash 409 $ echo 'foo' | csvq calc "base64_encode(c1)" 410 Zm9v 411 $ echo 123 | csvq calc "c1 + 100" 412 223 413 ``` 414 415 ### Check Update Subcommand 416 {: #check-update} 417 418 Check for updates. 419 ```bash 420 csvq [options] check-update [subcommand options] 421 ``` 422 423 ### Fields Subcommand 424 {: #fields} 425 426 Show fields in a file. 427 ```bash 428 csvq [options] fields CSV_FILE_PATH 429 ``` 430 431 ### Syntax Subcommand 432 {: #syntax} 433 434 Print syntax. 435 ```bash 436 csvq [options] syntax [search_word ...] 437 ``` 438 439 #### Subcommand Options 440 441 --include-pre-release 442 : Check including pre-release version. 443 444 445 ## Configurations 446 {: #configurations} 447 448 Before an execution of csvq, the following processings will be performed. 449 450 1. Load Environment Configurations. 451 2. Execute Pre-Load Statements. 452 3. Overwrite Flags with Command Options. 453 454 ### Environment Configurations 455 456 Configuration Files are loaded in the following order. 457 458 1. $XDG_CONFIG_HOME/csvq/csvq_env.json or $HOME/.config/csvq/csvq_env.json 459 2. $HOME/.csvq_env.json 460 3. $HOME/.csvq/csvq_env.json 461 4. CURRENT_DIRECTORY/csvq_env.json 462 463 Download a JSON file from [https://github.com/mithrandie/csvq/blob/master/csvq_env.json](https://github.com/mithrandie/csvq/blob/master/csvq_env.json) and put it to the above paths. 464 If the files exists in the multiple paths, then all existing files will be loaded and configuration items are overwritten with the values in the file that is loaded later. 465 466 #### Configuration Items in JSON 467 468 | Item | Format | default | 469 |:------------------------------------|:-----------------------------------|:--------------| 470 | datetime_format | array of strings | | 471 | timezone | string | Local | 472 | ansi_quotes | bool | false | 473 | interactive_shell.history_file | string | .csvq_history | 474 | interactive_shell.history_limit | number | 500 | 475 | interactive_shell.prompt | string | | 476 | interactive_shell.continuous_prompt | string | | 477 | interactive_shell.completion | bool | true | 478 | interactive_shell.kill_whole_line | bool | false | 479 | interactive_shell.vi_mode | bool | false | 480 | environment_variables | object{var_name: string} || 481 | palette.effectors | object{effect_name: effect_object} || 482 483 ##### Interactive Shell 484 485 Except for _Prompt_ and _Continuous Prompt_, items in this category are effective only on the following systems. 486 - darwin dragonfly freebsd linux netbsd openbsd solaris windows 487 488 ###### History File 489 490 File name to save command history. If it is a relative path, then the path is interpreted as a relative path from your home directory. 491 On UNIX-like systems, you can use environment variable such as $HOME or ${HOME} to specify the path. 492 If not, '~' can be used to represent home directory. 493 494 ###### History Limit 495 496 Max length of command history. 497 If _history_limit_ is set to -1, then the command history is disabled. 498 499 ###### Prompt, Continuous Prompt 500 501 Appearance of the prompt on the interactive shell. 502 You can embed some expressions in the appearance by [using a Dollar Sign(U+0024 `$`) and Curly Brackets(U+007B, U+007D `{}`)]({{ '/reference/external-command.html#embedded-expression' | relative_url }}) 503 504 ###### Completion 505 506 Whether to use completion. 507 508 ###### Kill Whole Line 509 510 If true then keyboard shortcut "Ctrl+U" will remove the entire line. 511 If not, the shortcut will remove before the current cursor position on the line. 512 513 ###### Vi Mode 514 515 Whether to use vi-mode. 516 517 ##### Effect Object 518 519 ###### Effects 520 521 Effects are specified with an array of strings. Following strings are supported. 522 523 - Reset Bold Faint Italic Underline SlowBlink RapidBlink ReverseVideo Conceal CrossedOut 524 525 ###### Colors 526 527 Foreground and background colors are specified with one of the following values. 528 529 8 Color (String) 530 : Following strings are supported. 531 532 - Black Red Green Yellow Blue Magenta Cyan White BrightBlack BrightRed BrightGreen BrightYellow BrightBlue BrightMagenta BrightCyan BrightWhite DefaultColor 533 534 256 Color (Number) 535 : Color number from 0 to 255. 536 537 RGB Color (Array of 3 numbers) 538 : The first element is the intensity of red between 0 and 255, the second is green, and the third is blue. 539 540 541 ### Pre-Load Statements 542 543 Files in which statements are written will be loaded and executed in the following order. 544 545 1. $XDG_CONFIG_HOME/csvq/csvqrc or $HOME/.config/csvq/csvqrc 546 2. $HOME/.csvqrc 547 3. $HOME/.csvq/csvqrc 548 4. CURRENT_DIRECTORY/csvqrc 549 550 551 ## Special Characters 552 {: #special_characters} 553 554 In command parameters and statements, following strings represent special characters. 555 556 | string | unicode character | 557 |:-------|:------------------------------------------| 558 | \a | U+0007 Bell | 559 | \b | U+0008 Backspace | 560 | \f | U+000C Form Feed | 561 | \n | U+000A Line Feed | 562 | \r | U+000D Carriage Return | 563 | \t | U+0009 Horizontal Tab | 564 | \v | U+000b Vertical Tab | 565 | \\" | U+0022 Double Quote | 566 | \\' | U+0027 Single Quote (in strings only) | 567 | \\\` | U+0060 Grave Accent (in identifiers only) | 568 | \\\\ | U+005c Backslash | 569 570 571 ### Escaped Characters in JSON output 572 {: #escaped_characters_in_json} 573 574 | character | BACKSLASH | HEX | HEXALL | 575 |:-----------------------|:----------|:-------|:-------------------------------------| 576 | U+0022 Quotation Mark | \\" | \u0022 | \u0022 | 577 | U+005C Backslash | \\\\ | \u005C | \u005C | 578 | U+002F Solidus | \/ | \u002F | \u002F | 579 | U+0008 Backspace | \b | \u0008 | \u0008 | 580 | U+000C Form Feed | \f | \u000C | \u000C | 581 | U+000A Line Feed | \n | \u000A | \u000A | 582 | U+000D Carriage Return | \r | \u000D | \u000D | 583 | U+0009 Horizontal Tab | \t | \u0009 | \u0009 | 584 | U+0000 - U+001F | \uXXXX | \uXXXX | \uXXXX | 585 | - U+FFFF | N/A | N/A | \uXXXX | 586 | U+10000 - | N/A | N/A | \uXXXX\uXXXX (UTF-16 surrogate pair) | 587 588 ## Return Code 589 {: #return_code} 590 591 | code | description | 592 |--------:|:---------------------------------------------------------------------------------------------------| 593 | 0 | Normally terminated | 594 | 1 | Errors inside the csvq | 595 | 2 | Incorrect command usage | 596 | 4 | Syntax errors | 597 | 8 | Timeout errors | 598 | 16 | I/O errors | 599 | 32 | Errors outside the csvq | 600 | 64 | The default of triggered errors by [TRIGGER ERROR]({{ '/reference/control-flow.html#trigger_error' | relative_url }}) statements | 601 | 128+_n_ | Terminated by signal "_n_" | 602