Skip to content

sean-stapleton-doyle/csvtk

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

csvtk

A cli toolkit for working with CSV files.

Features

  • View - Interactive terminal viewer with keyboard navigation, horizontal scrolling, row selection, copy, and in-viewer filtering
  • Count - Count rows and columns
  • Move - Reorder rows and columns
  • Header - Display and examine CSV headers
  • Rename - Rename column headers
  • Convert - Convert between CSV and TSV formats
  • Lint - Validate CSV files according to RFC 4180
  • Filter - Filtering with regex and numeric comparisons
  • Select - Extract specific columns
  • Sort - Sort data by column values
  • Transform - Transform data (uppercase, lowercase, replace, trim)
  • Stdin Support - All commands support stdin for easy command chaining

Installation

go build -o csvtk

Usage

All commands support reading from stdin using - or by omitting the filename.

View CSV Files

Open a basic viewer:

csvtk view myfile.csv
# Or simply:
csvtk myfile.csv

Keyboard shortcuts:

  • ↑↓/jk: Move up/down one row
  • ←→/hl: Scroll left/right through columns
  • PgUp/PgDn: Navigate by page
  • g/home: Jump to first row
  • G/end: Jump to last row
  • c: Copy selected row to clipboard
  • f: Enter filter mode (search across all columns)
  • r: Reset/clear filter
  • q: Quit

Filter mode:

  • Type to search (case-insensitive, searches all columns)
  • Enter: Apply filter
  • Esc: Cancel filter

Count Operations

Count rows (excluding header):

csvtk count rows myfile.csv
# Or from stdin:
cat myfile.csv | csvtk count rows

Count columns:

csvtk count columns myfile.csv

Move Operations

Move a column to a different position (uses an index to specify the new location):

csvtk move column Email 0 myfile.csv

Move a row to a different position:

csvtk move row 5 0 myfile.csv

Header Operations

Display header:

csvtk header myfile.csv
# Or from stdin:
cat myfile.csv | csvtk header

Display header with column indices:

csvtk header -n myfile.csv

Rename a header:

csvtk rename Email EmailAddress myfile.csv -o updated.csv

Convert Formats

Convert CSV to TSV:

csvtk convert myfile.csv --to-tsv

Convert TSV to CSV:

csvtk convert myfile.tsv --to-csv

Auto-detect format and convert:

csvtk convert myfile.csv  # Automatically converts to TSV

Lint CSV Files

Validate CSV file structure:

csvtk lint myfile.csv

Use lazy quotes for less strict parsing:

csvtk lint --lazy-quotes myfile.csv

Filter Operations

csvtk supports powerful filtering with multiple strategies:

String Filters:

# Exact match (default)
csvtk filter City "New York" data.csv

# Contains
csvtk filter Name "John" data.csv --operator contains

# Starts with
csvtk filter Email "admin" data.csv --operator starts-with

# Ends with
csvtk filter Email ".com" data.csv --operator ends-with

# Not equals
csvtk filter Status "inactive" data.csv --operator not-equals

Regex Filters:

# Regex matching
csvtk filter Email "@(gmail|yahoo)\.com" data.csv --regex

# Using operator flag
csvtk filter Phone "^\d{3}-\d{3}-\d{4}$" data.csv --operator regex

Numeric Filters:

# Greater than
csvtk filter Age 30 data.csv --operator ">"

# Less than
csvtk filter Price 100 data.csv --operator "<"

# Greater than or equal
csvtk filter Score 85 data.csv --operator ">="

# Less than or equal
csvtk filter Quantity 50 data.csv --operator "<="

# Numeric equality
csvtk filter Count 10 data.csv --operator "=="

# Numeric not equal
csvtk filter Amount 0 data.csv --operator "!="

Output to file:

csvtk filter Age 30 data.csv --operator ">" -o adults.csv

Filter from stdin:

cat data.csv | csvtk filter Age 30 - --operator ">" > adults.csv

Select Columns

Extract specific columns:

csvtk select "Name,Email,Age" myfile.csv

Save to file:

csvtk select "Name,Email" myfile.csv -o output.csv

From stdin:

cat data.csv | csvtk select "Name,Age" -

Sort Data

Sort by column (ascending):

csvtk sort Age myfile.csv -o sorted.csv

Sort in descending order:

csvtk sort Age myfile.csv -r -o sorted.csv

Sort to stdout:

csvtk sort Name myfile.csv
# Or from stdin:
cat myfile.csv | csvtk sort City -

Transform Data

Uppercase:

# Transform specific column
csvtk transform upper Name data.csv -o output.csv

# Transform all columns
csvtk transform upper --all data.csv

Lowercase:

csvtk transform lower Email data.csv

Replace text:

# Replace in specific column
csvtk transform replace "@example.com" "@newdomain.com" Email data.csv

# Replace in all columns
csvtk transform replace "old" "new" --all data.csv -o output.csv

Trim whitespace:

csvtk transform trim Name data.csv
csvtk transform trim --all data.csv

Transform with stdin:

cat data.csv | csvtk transform lower Name - > output.csv

Command Chaining Examples

One of the most powerful features is the ability to chain commands using stdin/stdout:

# Filter, select columns, and sort in one pipeline
cat data.csv | \
  csvtk filter Age 25 - --operator ">" | \
  csvtk select "Name,Email,Age" - | \
  csvtk sort Age - > result.csv

# Transform, rename, and output
csvtk transform lower Email data.csv | \
  csvtk rename Email EmailAddress - | \
  csvtk transform replace "@example.com" "@newdomain.com" EmailAddress - \
  > transformed.csv

# Count filtered results
csvtk filter City "New York" data.csv | \
  csvtk count rows

# Complex filtering with multiple conditions (using shell)
csvtk filter Age 30 data.csv --operator ">" | \
  csvtk filter City "New York" - | \
  csvtk select "Name,Age" - | \
  csvtk sort Age - -r

Global Flags

Most commands support these flags:

  • -d, --delimiter: Specify field delimiter (default: ,)
    • Use \t or \\t for tab-delimited files
  • -o, --output: Specify output file (defaults to stdout for most commands)

Filter Operators

The filter command supports the following operators via the --operator flag:

String operators:

  • equals or eq - Exact string match (default)
  • contains - Substring match
  • starts-with or startswith - Prefix match
  • ends-with or endswith - Suffix match
  • not-equals or ne - String inequality
  • regex or regexp - Regular expression match

Numeric operators:

  • > or gt - Greater than
  • < or lt - Less than
  • >= or gte - Greater than or equal
  • <= or lte - Less than or equal
  • == - Numeric equality
  • != - Numeric inequality

License

See LICENSE file for details.

Contributing

Contributions are welcome! Please feel free to submit issues or pull requests.

About

csvtk is a cli csv toolkit for performing various manipulation and transforms to csvs on the command line

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages