All Projects → shenwei356 → Csvtk

shenwei356 / Csvtk

Licence: mit
A cross-platform, efficient and practical CSV/TSV toolkit in Golang

Programming Languages

go
31211 projects - #10 most used programming language
golang
3204 projects

Projects that are alternatives of or similar to Csvtk

Seqkit
A cross-platform and ultrafast toolkit for FASTA/Q file manipulation in Golang
Stars: ✭ 607 (+7.24%)
Mutual labels:  bioinformatics, toolkit, cross-platform, tool
Q
q - Run SQL directly on CSV or TSV files
Stars: ✭ 8,809 (+1456.36%)
Mutual labels:  command-line, csv, tsv
Proji
A powerful cross-platform CLI project templating tool.
Stars: ✭ 156 (-72.44%)
Mutual labels:  command-line, cross-platform, tool
Csview
📠 A high performance csv viewer with cjk/emoji support.
Stars: ✭ 208 (-63.25%)
Mutual labels:  command-line, csv, tool
Tsv Utils
eBay's TSV Utilities: Command line tools for large, tabular data files. Filtering, statistics, sampling, joins and more.
Stars: ✭ 1,215 (+114.66%)
Mutual labels:  command-line, csv, tsv
Miller
Miller is like awk, sed, cut, join, and sort for name-indexed data such as CSV, TSV, and tabular JSON
Stars: ✭ 4,633 (+718.55%)
Mutual labels:  command-line, csv, tsv
Micro
A modern and intuitive terminal-based text editor
Stars: ✭ 18,526 (+3173.14%)
Mutual labels:  command-line, cross-platform
Pastel
A command-line tool to generate, analyze, convert and manipulate colors
Stars: ✭ 3,742 (+561.13%)
Mutual labels:  command-line, tool
Fd
A simple, fast and user-friendly alternative to 'find'
Stars: ✭ 19,851 (+3407.24%)
Mutual labels:  command-line, tool
Visidata
A terminal spreadsheet multitool for discovering and arranging data
Stars: ✭ 4,606 (+713.78%)
Mutual labels:  csv, tsv
Onefetch
Git repository summary on your terminal
Stars: ✭ 3,680 (+550.18%)
Mutual labels:  command-line, tool
Rainbow csv
🌈Rainbow CSV - Vim plugin: Highlight columns in CSV and TSV files and run queries in SQL-like language
Stars: ✭ 337 (-40.46%)
Mutual labels:  csv, tsv
Rush
A cross-platform command-line tool for executing jobs in parallel
Stars: ✭ 421 (-25.62%)
Mutual labels:  bioinformatics, cross-platform
Sq
swiss-army knife for data
Stars: ✭ 275 (-51.41%)
Mutual labels:  csv, tsv
Teip
Select partial standard input and replace with the result of another command efficiently
Stars: ✭ 280 (-50.53%)
Mutual labels:  command-line, tool
Structopt
Parse command line arguments by defining a struct
Stars: ✭ 323 (-42.93%)
Mutual labels:  command-line, cross-platform
Flatfiles
Reads and writes CSV, fixed-length and other flat file formats with a focus on schema definition, configuration and speed.
Stars: ✭ 275 (-51.41%)
Mutual labels:  csv, tsv
Deeppurpose
A Deep Learning Toolkit for DTI, Drug Property, PPI, DDI, Protein Function Prediction (Bioinformatics)
Stars: ✭ 342 (-39.58%)
Mutual labels:  bioinformatics, toolkit
Nigui
Cross-platform desktop GUI toolkit written in Nim
Stars: ✭ 430 (-24.03%)
Mutual labels:  toolkit, cross-platform
Jtc
JSON processing utility
Stars: ✭ 425 (-24.91%)
Mutual labels:  command-line, tool

csvtk - A cross-platform, efficient and practical CSV/TSV toolkit

Introduction

Similar to FASTA/Q format in field of Bioinformatics, CSV/TSV formats are basic and ubiquitous file formats in both Bioinformatics and data science.

People usually use spreadsheet software like MS Excel to process table data. However this is all by clicking and typing, which is not automated and is time-consuming to repeat, especially when you want to apply similar operations with different datasets or purposes.

You can also accomplish some CSV/TSV manipulations using shell commands, but more code is needed to handle the header line. Shell commands do not support selecting columns with column names either.

csvtk is convenient for rapid data investigation and also easy to integrate into analysis pipelines. It could save you lots of time in (not) writing Python/R scripts.

Table of Contents

Features

  • Cross-platform (Linux/Windows/Mac OS X/OpenBSD/FreeBSD)
  • Light weight and out-of-the-box, no dependencies, no compilation, no configuration
  • Fast, multiple-CPUs supported (some commands)
  • Practical functions provided by N subcommands
  • Support STDIN and gziped input/output file, easy being used in pipe
  • Most of the subcommands support unselecting fields and fuzzy fields, e.g. -f "-id,-name" for all fields except "id" and "name", -F -f "a.*" for all fields with prefix "a.".
  • Support some common plots (see usage)
  • Seamlessly support for data with meta line (e.g., sep=,) of separator declaration used by MS Excel

Subcommands

47 subcommands in total.

Information

  • headers: prints headers
  • dim: dimensions of CSV file
  • nrow: print number of records
  • ncol: print number of columns
  • summary: summary statistics of selected numeric or text fields (groupby group fields)
  • watch: online monitoring and histogram of selected field
  • corr: calculate Pearson correlation between numeric columns

Format conversion

  • pretty: converts CSV to readable aligned table
  • csv2tab: converts CSV to tabular format
  • tab2csv: converts tabular format to CSV
  • space2tab: converts space delimited format to CSV
  • transpose: transposes CSV data
  • csv2md: converts CSV to markdown format
  • csv2json: converts CSV to JSON format
  • csv2xlsx: convert CSV/TSV files to XLSX file
  • xlsx2csv: converts XLSX to CSV format

Set operations

  • head: prints first N records
  • concat: concatenates CSV/TSV files by rows
  • sample: sampling by proportion
  • cut: select and arrange fields
  • grep: greps data by selected fields with patterns/regular expressions
  • uniq: unique data without sorting
  • freq: frequencies of selected fields
  • inter: intersection of multiple files
  • filter: filters rows by values of selected fields with arithmetic expression
  • filter2: filters rows by awk-like arithmetic/string expressions
  • join: join files by selected fields (inner, left and outer join)
  • split splits CSV/TSV into multiple files according to column values
  • splitxlsx: splits XLSX sheet into multiple sheets according to column values
  • comb: compute combinations of items at every row

Edit

  • add-header: add column names
  • del-header: delete column names
  • rename: renames column names with new names
  • rename2: renames column names by regular expression
  • replace: replaces data of selected fields by regular expression
  • round: round float to n decimal places
  • mutate: creates new columns from selected fields by regular expression
  • mutate2: creates new column from selected fields by awk-like arithmetic/string expressions
  • sep: separate column into multiple columns
  • gather: gathers columns into key-value pairs
  • unfold: unfold multiple values in cells of a field
  • fold: fold multiple values of a field into cells of groups

Ordering

  • sort: sorts by selected fields

Ploting

Misc

  • cat stream file and report progress
  • version print version information and check for update
  • genautocomplete generate shell autocompletion script (bash|zsh|fish|powershell)

Installation

Download Page

csvtk is implemented in Go programming language, executable binary files for most popular operating systems are freely available in release page.

Method 1: Download binaries (latest stable/dev version)

Just download compressed executable file of your operating system, and decompress it with tar -zxvf *.tar.gz command or other tools. And then:

  1. For Linux-like systems

    1. If you have root privilege simply copy it to /usr/local/bin:

       sudo cp csvtk /usr/local/bin/
      
    2. Or copy to anywhere in the environment variable PATH:

       mkdir -p $HOME/bin/; cp csvtk $HOME/bin/
      
  2. For windows, just copy csvtk.exe to C:\WINDOWS\system32.

Method 2: Install via conda (latest stable version) Anaconda Cloud downloads

conda install -c bioconda csvtk

Method 3: For Go developer (latest stable/dev version)

go get -u github.com/shenwei356/csvtk/csvtk

Method 4: For ArchLinux AUR users (may be not the latest)

yaourt -S csvtk

Command-line completion

Bash:

# generate completion shell
csvtk genautocomplete --shell bash

# configure if never did.
# install bash-completion if the "complete" command is not found.
echo "for bcfile in ~/.bash_completion.d/* ; do source \$bcfile; done" >> ~/.bash_completion
echo "source ~/.bash_completion" >> ~/.bashrc

Zsh:

# generate completion shell
csvtk genautocomplete --shell zsh --file ~/.zfunc/_csvtk

# configure if never did
echo 'fpath=( ~/.zfunc "${fpath[@]}" )' >> ~/.zshrc
echo "autoload -U compinit; compinit" >> ~/.zshrc

fish:

csvtk genautocomplete --shell fish --file ~/.config/fish/completions/csvtk.fish

Compared to csvkit

csvkit, attention: this table wasn't updated for 2 years.

Features csvtk csvkit Note
Read Gzip Yes Yes read gzip files
Fields ranges Yes Yes e.g. -f 1-4,6
Unselect fileds Yes -- e.g. -1 for excluding first column
Fuzzy fields Yes -- e.g. ab* for columns with name prefix "ab"
Reorder fields Yes Yes it means -f 1,2 is different from -f 2,1
Rename columns Yes -- rename with new name(s) or from existed names
Sort by multiple keys Yes Yes bash sort like operations
Sort by number Yes -- e.g. -k 1:n
Multiple sort Yes -- e.g. -k 2:r -k 1:nr
Pretty output Yes Yes convert CSV to readable aligned table
Unique data Yes -- unique data of selected fields
frequency Yes -- frequencies of selected fields
Sampling Yes -- sampling by proportion
Mutate fields Yes -- create new columns from selected fields
Repalce Yes -- replace data of selected fields

Similar tools:

  • csvkit - A suite of utilities for converting to and working with CSV, the king of tabular file formats. http://csvkit.rtfd.org/
  • xsv - A fast CSV toolkit written in Rust.
  • miller - Miller is like sed, awk, cut, join, and sort for name-indexed data such as CSV and tabular JSON http://johnkerl.org/miller
  • tsv-utils - Command line utilities for tab-separated value files written in the D programming language.

Examples

More examples and tutorial.

Attention

  1. The CSV parser requires all the lines have same number of fields/columns. Even lines with spaces will cause error. Use '-I/--ignore-illegal-row' to skip these lines if neccessary.
  2. By default, csvtk thinks your files have header row, if not, switch flag -H on.
  3. Column names better be unique.
  4. By default, lines starting with # will be ignored, if the header row starts with #, please assign flag -C another rare symbol, e.g. '$'.
  5. By default, csvtk handles CSV files, use flag -t for tab-delimited files.
  6. If " exists in tab-delimited files, use flag -l.
  7. Do not mix use field (column) numbers and names.

Examples

  1. Pretty result

     $ csvtk pretty names.csv
     id   first_name   last_name   username
     11   Rob          Pike        rob
     2    Ken          Thompson    ken
     4    Robert       Griesemer   gri
     1    Robert       Thompson    abc
     NA   Robert       Abel        123
    
  2. Summary of selected numeric fields, supporting "group-by"

     $ cat testdata/digitals2.csv \
         | csvtk summary --ignore-non-digits --fields f4:sum,f5:sum --groups f1,f2 \
         | csvtk pretty
     f1    f2     f4:sum   f5:sum
     bar   xyz    7.00     106.00
     bar   xyz2   4.00     4.00
     foo   bar    6.00     3.00
     foo   bar2   4.50     5.00
    
  3. Select fields/columns (cut)

    • By index: csvtk cut -f 1,2
    • By names: csvtk cut -f first_name,username
    • Unselect: csvtk cut -f -1,-2 or csvtk cut -f -first_name
    • Fuzzy fields: csvtk cut -F -f "*_name,username"
    • Field ranges: csvtk cut -f 2-4 for column 2,3,4 or csvtk cut -f -3--1 for discarding column 1,2,3
    • All fields: csvtk cut -F -f "*"
  4. Search by selected fields (grep) (matched parts will be highlighted as red)

    • By exactly matching: csvtk grep -f first_name -p Robert -p Rob
    • By regular expression: csvtk grep -f first_name -r -p Rob
    • By pattern list: csvtk grep -f first_name -P name_list.txt
    • Remore rows containing missing data (NA): csvtk grep -F -f "*" -r -p "^$" -v
  5. Rename column names (rename and rename2)

    • Setting new names: csvtk rename -f A,B -n a,b or csvtk rename -f 1-3 -n a,b,c
    • Replacing with original names by regular express: cat ../testdata/c.csv | ./csvtk rename2 -F -f "*" -p "(.*)" -r 'prefix_$1' for adding prefix to all column names.
  6. Edit data with regular expression (replace)

    • Remove Chinese charactors: csvtk replace -F -f "*_name" -p "\p{Han}+" -r ""
  7. Create new column from selected fields by regular expression (mutate)

    • In default, copy a column: csvtk mutate -f id
    • Extract prefix of data as group name (get "A" from "A.1" as group name): csvtk mutate -f sample -n group -p "^(.+?)\."
  8. Sort by multiple keys (sort)

    • By single column : csvtk sort -k 1 or csvtk sort -k last_name
    • By multiple columns: csvtk sort -k 1,2 or csvtk sort -k 1 -k 2 or csvtk sort -k last_name,age
    • Sort by number: csvtk sort -k 1:n or csvtk sort -k 1:nr for reverse number
    • Complex sort: csvtk sort -k region -k age:n -k id:nr
    • In natural order: csvtk sort -k chr:N
  9. Join multiple files by keys (join)

    • All files have same key column: csvtk join -f id file1.csv file2.csv
    • Files have different key columns: csvtk join -f "username;username;name" names.csv phone.csv adress.csv -k
  10. Filter by numbers (filter)

    • Single field: csvtk filter -f "id>0"
    • Multiple fields: csvtk filter -f "1-3>0"
    • Using --any to print record if any of the field satisfy the condition: csvtk filter -f "1-3>0" --any
    • fuzzy fields: csvtk filter -F -f "A*!=0"
  11. Filter rows by awk-like arithmetic/string expressions (filter2)

    • Using field index: csvtk filter2 -f '$3>0'
    • Using column names: csvtk filter2 -f '$id > 0'
    • Both arithmetic and string expressions: csvtk filter2 -f '$id > 3 || $username=="ken"'
    • More complicated: csvtk filter2 -H -t -f '$1 > 2 && $2 % 2 == 0'
  12. Ploting

    • plot histogram with data of the second column:

        csvtk -t plot hist testdata/grouped_data.tsv.gz -f 2 | display
      

      histogram.png

    • plot boxplot with data of the "GC Content" (third) column, group information is the "Group" column.

        csvtk -t plot box testdata/grouped_data.tsv.gz -g "Group" \
            -f "GC Content" --width 3 | display
      

      boxplot.png

    • plot horiz boxplot with data of the "Length" (second) column, group information is the "Group" column.

       csvtk -t plot box testdata/grouped_data.tsv.gz -g "Group" -f "Length"  \
           --height 3 --width 5 --horiz --title "Horiz box plot" | display
      

    boxplot2.png

    • plot line plot with X-Y data

        csvtk -t plot line testdata/xy.tsv -x X -y Y -g Group | display
      

      lineplot.png

    • plot scatter plot with X-Y data

        csvtk -t plot line testdata/xy.tsv -x X -y Y -g Group --scatter | display
      

      scatter.png

Acknowledgements

We are grateful to Zhiluo Deng and Li Peng for suggesting features and reporting bugs.

Thanks Albert Vilella for features suggestion, which makes csvtk feature-rich。

Contact

Create an issue to report bugs, propose new functions or ask for help.

Or leave a comment.

License

MIT License

Starchart

Stargazers over time
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].