LinuxCommandLibrary

csvtool

Manipulate CSV (Comma Separated Value) files

TLDR

Extract the second column from a CSV file

$ csvtool [[-c|--column]] [2] [path/to/file.csv]
copy

Extract the second and fourth columns from a CSV file
$ csvtool [[-c|--column]] [2,4] [path/to/file.csv]
copy

Extract lines from a CSV file where the second column exactly matches String
$ csvtool [[-c|--column]] [2] [[-s|--search]] '[^String$]' [path/to/file.csv]
copy

Extract lines from a CSV file where the second column starts with Bar
$ csvtool [[-c|--column]] [2] [[-s|--search]] '[^Bar]' [path/to/file.csv]
copy

Find lines in a CSV file where the second column ends with Baz and then extract the third and sixth columns
$ csvtool [[-c|--column]] [2] [[-s|--search]] '[Baz$]' [path/to/file.csv] | csvtool [[-e|--no-header]] [[-c|--column]] [3,6]
copy

SYNOPSIS

csvtool [options...] [filespec] [command [args...]]

PARAMETERS

--help
    Display usage summary.

--version
    Print version information.

-t DELIM, --delimiter=DELIM
    Set input field delimiter (default: auto-detect).

--tab
    Use TAB as input delimiter.

--fields=F1[,F2...]
    Output only specified input fields (1-based indices or header names).

--filter=EXPR
    Output rows where Perl expression EXPR evaluates true.

--skip=N
    Skip first N lines.

--head=N
    Output first N lines.

--tail=N
    Output last N lines.

--trim
    Trim leading/trailing whitespace from fields.

--tolower
    Convert all fields to lowercase.

--toupper
    Convert all fields to uppercase.

--debug
    Enable debug output.

--quiet
    Suppress non-essential output.

DESCRIPTION

csvtool is a lightweight Perl-based command-line tool for processing CSV files on Linux/Unix systems. It enables precise extraction of columns by number or header name, row filtering with Perl expressions, and various transformations like case conversion and trimming. Unlike cut(1), which splits on fixed positions, csvtool parses CSV intelligently, handling quoted fields, embedded delimiters, and custom separators (comma, tab, etc.).

Typical workflows include selecting subsets of data for reports, cleaning datasets, or piping into other tools. It supports stdin/stdout for pipelines, skipping headers, limiting output lines, and more. Ideal for shell scripts, data analysis, and quick ETL tasks without heavy dependencies.

While not as feature-rich as modern suites like csvkit, its simplicity and speed make it a staple for ad-hoc CSV wrangling. Available in repositories like Debian/Ubuntu via the csvtool package.

CAVEATS

Assumes well-formed CSV; may struggle with malformed or very large files; Perl expressions require basic regex knowledge; no built-in support for output formatting like csvlook.

COMMON EXAMPLES

csvtool --fields 1,3 data.csv
Extract columns 1 and 3.

csvtool --tab --fields Name,Age data.tsv
Select by header names from TSV.

csvtool --filter '$age > 30' data.csv
Filter rows where age > 30 (uses Perl vars).

HISTORY

Developed by Garrick Brian around 2004 as a Perl script for easy CSV handling. Hosted at garrick.org/csvtool; included in Debian since 2006. Maintained sporadically, remains popular for its minimalism amid rise of csvkit (2010s).

SEE ALSO

csvcut(1), cut(1), awk(1), csvkit(1), miller(1)

Copied to clipboard