diff --git a/README.md b/README.md index fed068a..09cb514 100644 --- a/README.md +++ b/README.md @@ -40,6 +40,7 @@ It could save you much time of writing Python/R scripts. - [Features](#features) - [Subcommands](#subcommands) - [Installation](#installation) +- [Bash-completion](#bash-completion) - [Compared to `csvkit`](#compared-to-csvkit) - [Examples](#examples) - [Acknowledgements](#acknowledgements) @@ -114,6 +115,12 @@ It could save you much time of writing Python/R scripts. - `plot box` boxplot - `plot line` line plot and scatter plot +**Misc** + +- `version` print version information and check for update +- `genautocomplete` generate shell autocompletion script + + ## Installation [Download Page](https://github.com/shenwei356/csvtk/releases) @@ -152,6 +159,25 @@ And then: go get -u github.com/shenwei356/csvtk/csvtk +## Bash-completion + +Note: The current version supports Bash only. +This should work for *nix systems with Bash installed. + +Howto: + +1. run: `csvtk genautocomplete` + +2. create and edit `~/.bash_completion` file if you don't have it. + + nano ~/.bash_completion + + add the following: + + for bcfile in ~/.bash_completion.d/* ; do + . $bcfile + done + ## Compared to `csvkit` [csvkit](http://csvkit.readthedocs.org/) diff --git a/csvtk/cmd/genautocomplete.go b/csvtk/cmd/genautocomplete.go new file mode 100644 index 0000000..f0f025d --- /dev/null +++ b/csvtk/cmd/genautocomplete.go @@ -0,0 +1,83 @@ +// Copyright © 2016 Wei Shen +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE. + +package cmd + +import ( + "fmt" + "os" + "path/filepath" + + homedir "github.com/mitchellh/go-homedir" + "github.com/shenwei356/util/pathutil" + "github.com/spf13/cobra" +) + +// genautocompleteCmd represents the fq2fa command +var genautocompleteCmd = &cobra.Command{ + Use: "genautocomplete", + Short: "generate shell autocompletion script", + Long: `generate shell autocompletion script + +Note: The current version supports Bash only. +This should work for *nix systems with Bash installed. + +Howto: + +1. run: csvtk genautocomplete + +2. create and edit ~/.bash_completion file if you don't have it. + + nano ~/.bash_completion + + add the following: + + for bcfile in ~/.bash_completion.d/* ; do + . $bcfile + done + +`, + Run: func(cmd *cobra.Command, args []string) { + autocompleteTarget := getFlagString(cmd, "file") + autocompleteType := getFlagString(cmd, "type") + + if autocompleteType != "bash" { + checkError(fmt.Errorf("only Bash is supported for now")) + } + + dir := filepath.Dir(autocompleteTarget) + ok, err := pathutil.DirExists(dir) + checkError(err) + if !ok { + os.MkdirAll(dir, 0744) + } + checkError(cmd.Root().GenBashCompletionFile(autocompleteTarget)) + + log.Infof("bash completion file for csvtk saved to %s", autocompleteTarget) + }, +} + +func init() { + RootCmd.AddCommand(genautocompleteCmd) + defaultCompletionFile, err := homedir.Expand("~/.bash_completion.d/csvtk.sh") + checkError(err) + genautocompleteCmd.Flags().StringP("file", "", defaultCompletionFile, "autocompletion file") + genautocompleteCmd.Flags().StringP("type", "", "bash", "autocompletion type (currently only bash supported)") +} diff --git a/csvtk/cmd/helper.go b/csvtk/cmd/helper.go index d9a9832..69ac1c7 100644 --- a/csvtk/cmd/helper.go +++ b/csvtk/cmd/helper.go @@ -36,7 +36,7 @@ import ( ) // VERSION of csvtk -const VERSION = "0.8.0" +const VERSION = "0.9.0" func checkError(err error) { if err != nil { diff --git a/csvtk/cmd/mutate2.go b/csvtk/cmd/mutate2.go index c6bdfe3..24c263a 100644 --- a/csvtk/cmd/mutate2.go +++ b/csvtk/cmd/mutate2.go @@ -307,5 +307,5 @@ func init() { RootCmd.AddCommand(mutate2Cmd) mutate2Cmd.Flags().StringP("expression", "e", "", `artithmetic/string expressions. e.g. '$1 + $2', '$a / $b', ' $1 > 100 ? "big" : "small" '`) mutate2Cmd.Flags().StringP("name", "n", "", `new column name`) - mutate2Cmd.Flags().IntP("digits", "", 2, `number of digits after the dot`) + mutate2Cmd.Flags().IntP("digits", "L", 2, `number of digits after the dot`) } diff --git a/dev-version.md b/dev-version.md index 47afea3..e69de29 100644 --- a/dev-version.md +++ b/dev-version.md @@ -1 +0,0 @@ -- add subcommand `csvtk mutate2`: diff --git a/doc/docs/download.md b/doc/docs/download.md index 6000ccc..7afee80 100644 --- a/doc/docs/download.md +++ b/doc/docs/download.md @@ -6,18 +6,11 @@ ## Current Version -[csvtk v0.8.0](https://github.com/shenwei356/csvtk/releases/tag/v0.8.0) -[![Github Releases (by Release)](https://img.shields.io/github/downloads/shenwei356/csvtk/v0.8.0/total.svg)](https://github.com/shenwei356/csvtk/releases/tag/v0.8.0) - -- **new command `csvtk gather` for gathering columns into key-value pairs**. -- `csvtk sort`: support **sorting by user-defined order**. -- fix bug of *unselecting field*: wrongly reporting error of fields not existing. -affected commands: `cut`, `filter`, `fitler2`, `freq`, `grep`, `inter`, `mutate`, -`rename`, `rename2`, `replace`, `stats2`, `uniq`. -- update help message of flag `-F/--fuzzy-fields`. -- update help message of global flag `-t`, which overrides both `-d` and `-D`. - If you want other delimiter for tabular input, use `-t $'\t' -D "delimiter"`. +[csvtk v0.9.0](https://github.com/shenwei356/csvtk/releases/tag/v0.9.0) +[![Github Releases (by Release)](https://img.shields.io/github/downloads/shenwei356/csvtk/v0.9.0/total.svg)](https://github.com/shenwei356/csvtk/releases/tag/v0.9.0) +- add subcommand `csvtk mutate2`: create new column from selected fields by **awk-like artithmetic/string expressions** +- add new command `genautocomplete` to generate **shell autocompletion** script! Links: @@ -25,12 +18,12 @@ Links: OS |Arch |File, (mirror为中国用户下载镜像链接) |Download Count :------|:---------|:-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|:----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -Linux |32-bit |[csvtk_linux_386.tar.gz](https://github.com/shenwei356/csvtk/releases/download/v0.8.0/csvtk_linux_386.tar.gz), ([mirror](http://app.shenwei.me/data/csvtk/csvtk_linux_386.tar.gz)) |[![Github Releases (by Asset)](https://img.shields.io/github/downloads/shenwei356/csvtk/latest/csvtk_linux_386.tar.gz.svg?maxAge=3600)](https://github.com/shenwei356/csvtk/releases/download/v0.8.0/csvtk_linux_386.tar.gz) -Linux |**64-bit**|[**csvtk_linux_amd64.tar.gz**](https://github.com/shenwei356/csvtk/releases/download/v0.8.0/csvtk_linux_amd64.tar.gz), ([mirror](http://app.shenwei.me/data/csvtk/csvtk_linux_amd64.tar.gz)) |[![Github Releases (by Asset)](https://img.shields.io/github/downloads/shenwei356/csvtk/latest/csvtk_linux_amd64.tar.gz.svg?maxAge=3600)](https://github.com/shenwei356/csvtk/releases/download/v0.8.0/csvtk_linux_amd64.tar.gz) -OS X |32-bit |[csvtk_darwin_386.tar.gz](https://github.com/shenwei356/csvtk/releases/download/v0.8.0/csvtk_darwin_386.tar.gz), ([mirror](http://app.shenwei.me/data/csvtk/csvtk_darwin_386.tar.gz)) |[![Github Releases (by Asset)](https://img.shields.io/github/downloads/shenwei356/csvtk/latest/csvtk_darwin_386.tar.gz.svg?maxAge=3600)](https://github.com/shenwei356/csvtk/releases/download/v0.8.0/csvtk_darwin_386.tar.gz) -OS X |**64-bit**|[**csvtk_darwin_amd64.tar.gz**](https://github.com/shenwei356/csvtk/releases/download/v0.8.0/csvtk_darwin_amd64.tar.gz), ([mirror](http://app.shenwei.me/data/csvtk/csvtk_darwin_amd64.tar.gz)) |[![Github Releases (by Asset)](https://img.shields.io/github/downloads/shenwei356/csvtk/latest/csvtk_darwin_amd64.tar.gz.svg?maxAge=3600)](https://github.com/shenwei356/csvtk/releases/download/v0.8.0/csvtk_darwin_amd64.tar.gz) -Windows|32-bit |[csvtk_windows_386.exe.tar.gz](https://github.com/shenwei356/csvtk/releases/download/v0.8.0/csvtk_windows_386.exe.tar.gz), ([mirror](http://app.shenwei.me/data/csvtk/csvtk_windows_386.exe.tar.gz)) |[![Github Releases (by Asset)](https://img.shields.io/github/downloads/shenwei356/csvtk/latest/csvtk_windows_386.exe.tar.gz.svg?maxAge=3600)](https://github.com/shenwei356/csvtk/releases/download/v0.8.0/csvtk_windows_386.exe.tar.gz) -Windows|**64-bit**|[**csvtk_windows_amd64.exe.tar.gz**](https://github.com/shenwei356/csvtk/releases/download/v0.8.0/csvtk_windows_amd64.exe.tar.gz), ([mirror](http://app.shenwei.me/data/csvtk/csvtk_windows_amd64.exe.tar.gz))|[![Github Releases (by Asset)](https://img.shields.io/github/downloads/shenwei356/csvtk/latest/csvtk_windows_amd64.exe.tar.gz.svg?maxAge=3600)](https://github.com/shenwei356/csvtk/releases/download/v0.8.0/csvtk_windows_amd64.exe.tar.gz) +Linux |32-bit |[csvtk_linux_386.tar.gz](https://github.com/shenwei356/csvtk/releases/download/v0.9.0/csvtk_linux_386.tar.gz), ([mirror](http://app.shenwei.me/data/csvtk/csvtk_linux_386.tar.gz)) |[![Github Releases (by Asset)](https://img.shields.io/github/downloads/shenwei356/csvtk/latest/csvtk_linux_386.tar.gz.svg?maxAge=3600)](https://github.com/shenwei356/csvtk/releases/download/v0.9.0/csvtk_linux_386.tar.gz) +Linux |**64-bit**|[**csvtk_linux_amd64.tar.gz**](https://github.com/shenwei356/csvtk/releases/download/v0.9.0/csvtk_linux_amd64.tar.gz), ([mirror](http://app.shenwei.me/data/csvtk/csvtk_linux_amd64.tar.gz)) |[![Github Releases (by Asset)](https://img.shields.io/github/downloads/shenwei356/csvtk/latest/csvtk_linux_amd64.tar.gz.svg?maxAge=3600)](https://github.com/shenwei356/csvtk/releases/download/v0.9.0/csvtk_linux_amd64.tar.gz) +OS X |32-bit |[csvtk_darwin_386.tar.gz](https://github.com/shenwei356/csvtk/releases/download/v0.9.0/csvtk_darwin_386.tar.gz), ([mirror](http://app.shenwei.me/data/csvtk/csvtk_darwin_386.tar.gz)) |[![Github Releases (by Asset)](https://img.shields.io/github/downloads/shenwei356/csvtk/latest/csvtk_darwin_386.tar.gz.svg?maxAge=3600)](https://github.com/shenwei356/csvtk/releases/download/v0.9.0/csvtk_darwin_386.tar.gz) +OS X |**64-bit**|[**csvtk_darwin_amd64.tar.gz**](https://github.com/shenwei356/csvtk/releases/download/v0.9.0/csvtk_darwin_amd64.tar.gz), ([mirror](http://app.shenwei.me/data/csvtk/csvtk_darwin_amd64.tar.gz)) |[![Github Releases (by Asset)](https://img.shields.io/github/downloads/shenwei356/csvtk/latest/csvtk_darwin_amd64.tar.gz.svg?maxAge=3600)](https://github.com/shenwei356/csvtk/releases/download/v0.9.0/csvtk_darwin_amd64.tar.gz) +Windows|32-bit |[csvtk_windows_386.exe.tar.gz](https://github.com/shenwei356/csvtk/releases/download/v0.9.0/csvtk_windows_386.exe.tar.gz), ([mirror](http://app.shenwei.me/data/csvtk/csvtk_windows_386.exe.tar.gz)) |[![Github Releases (by Asset)](https://img.shields.io/github/downloads/shenwei356/csvtk/latest/csvtk_windows_386.exe.tar.gz.svg?maxAge=3600)](https://github.com/shenwei356/csvtk/releases/download/v0.9.0/csvtk_windows_386.exe.tar.gz) +Windows|**64-bit**|[**csvtk_windows_amd64.exe.tar.gz**](https://github.com/shenwei356/csvtk/releases/download/v0.9.0/csvtk_windows_amd64.exe.tar.gz), ([mirror](http://app.shenwei.me/data/csvtk/csvtk_windows_amd64.exe.tar.gz))|[![Github Releases (by Asset)](https://img.shields.io/github/downloads/shenwei356/csvtk/latest/csvtk_windows_amd64.exe.tar.gz.svg?maxAge=3600)](https://github.com/shenwei356/csvtk/releases/download/v0.9.0/csvtk_windows_amd64.exe.tar.gz) ## Installation @@ -69,9 +62,37 @@ And then: go get -u github.com/shenwei356/csvtk/csvtk +## Bash-completion + +Note: The current version supports Bash only. +This should work for *nix systems with Bash installed. + +Howto: + +1. run: `csvtk genautocomplete` + +2. create and edit `~/.bash_completion` file if you don't have it. + + nano ~/.bash_completion + + add the following: + + for bcfile in ~/.bash_completion.d/* ; do + . $bcfile + done ## Previous Versions +- [csvtk v0.8.0](https://github.com/shenwei356/csvtk/releases/tag/v0.8.0) +[![Github Releases (by Release)](https://img.shields.io/github/downloads/shenwei356/csvtk/v0.8.0/total.svg)](https://github.com/shenwei356/csvtk/releases/tag/v0.8.0) + - **new command `csvtk gather` for gathering columns into key-value pairs**. + - `csvtk sort`: support **sorting by user-defined order**. + - fix bug of *unselecting field*: wrongly reporting error of fields not existing. + affected commands: `cut`, `filter`, `fitler2`, `freq`, `grep`, `inter`, `mutate`, + `rename`, `rename2`, `replace`, `stats2`, `uniq`. + - update help message of flag `-F/--fuzzy-fields`. + - update help message of global flag `-t`, which overrides both `-d` and `-D`. + If you want other delimiter for tabular input, use `-t $'\t' -D "delimiter"`. - [csvtk v0.7.1](https://github.com/shenwei356/csvtk/releases/tag/v0.7.1) [![Github Releases (by Release)](https://img.shields.io/github/downloads/shenwei356/csvtk/v0.7.1/total.svg)](https://github.com/shenwei356/csvtk/releases/tag/v0.7.1) - `csvtk plot box` and `csvtk plot line`: fix bugs for special cases of input diff --git a/doc/docs/usage.md b/doc/docs/usage.md index 5da73f1..c9fa2f8 100644 --- a/doc/docs/usage.md +++ b/doc/docs/usage.md @@ -50,6 +50,7 @@ - [rename2](#rename2) - [replace](#replace) - [mutate](#mutate) +- [mutate2](#mutate2) - [gather](#gather) **Ordering** @@ -63,6 +64,10 @@ - [plot box](#plot-box) - [plot line](#plot-line) +**Misc** + +- [genautocomplete](#genautocomplete) + ## csvtk @@ -1190,6 +1195,69 @@ Examples ken,22222,k shenwei,999999,s +## mutate2 + +Usage + +``` +create new column from selected fields by awk-like artithmetic/string expressions + +The artithmetic/string expression is supported by: + + https://github.com/Knetic/govaluate + +Supported operators and types: + + Modifiers: + - / * & | ^ ** % >> << + Comparators: > >= < <= == != =~ !~ + Logical ops: || && + Numeric constants, as 64-bit floating point (12345.678) + String constants (single quotes: 'foobar') + Date constants (single quotes) + Boolean constants: true false + Parenthesis to control order of evaluation ( ) + Arrays (anything separated by , within parenthesis: (1, 2, 'foo')) + Prefixes: ! - ~ + Ternary conditional: ? : + Null coalescence: ?? + +Usage: + csvtk mutate2 [flags] + +Flags: + -L, --digits int number of digits after the dot (default 2) + -e, --expression string artithmetic/string expressions. e.g. '$1 + $2', '$a / $b', ' $1 > 100 ? "big" : "small" ' + -h, --help help for mutate2 + -n, --name string new column name + +``` + +Example + +1. Math + + $ cat testdata/digitals.tsv | csvtk mutate2 -t -H -e '$1 + $3' -L 0 + 4 5 6 10 + 1 2 3 4 + 7 8 0 7 + 8 1,000 4 12 + +1. Bool + + $ cat testdata/digitals.tsv | csvtk mutate2 -t -H -e '$1 > 5' + 4 5 6 false + 1 2 3 false + 7 8 0 true + 8 1,000 4 true + +1. Ternary conditional + + $ cat testdata/digitals.tsv | csvtk mutate2 -t -H -e '$1 > 5 ? "big" : "small" ' + 4 5 6 small + 1 2 3 small + 7 8 0 big + 8 1,000 4 big + ## gather Usage @@ -1532,7 +1600,39 @@ Examples ![scatter.png](testdata/figures/scatter.png) +## genautocomplete +Usage + +``` +generate shell autocompletion script + +Note: The current version supports Bash only. +This should work for *nix systems with Bash installed. + +Howto: + +1. run: csvtk genautocomplete + +2. create and edit ~/.bash_completion file if you don't have it. + + nano ~/.bash_completion + + add the following: + + for bcfile in ~/.bash_completion.d/* ; do + . $bcfile + done + +Usage: + csvtk genautocomplete [flags] + +Flags: + --file string autocompletion file (default "/home/shenwei/.bash_completion.d/csvtk.sh") + -h, --help help for genautocomplete + --type string autocompletion type (currently only bash supported) (default "bash") + +```