CSVCUT(1)

CSVCUT(1) πŸ’–

okay so bestie, welcome to csvcut, part of the csvkit family β€” this is the manual page, and I'm gonna walk you through it like we're texting at 2am πŸ¦‹


✨ NAME ✨

csvcut β€” literally just the csvcut documentation, no cap πŸ’…


🌸 DESCRIPTION 🌸

So what does it do? It filters and truncates CSV files. Think of it as the Unix cut command but for tabular data β€” iconic behavior honestly πŸ‘‘

Here's the whole usage situation, verbatim, don't @ me:

usage: csvcut [-h] [-d DELIMITER] [-t] [-q QUOTECHAR] [-u {0,1,2,3}] [-b]
              [-p ESCAPECHAR] [-z FIELD_SIZE_LIMIT] [-e ENCODING] [-S] [-H]
              [-K SKIP_LINES] [-v] [-l] [--zero] [-V] [-n] [-c COLUMNS]
              [-C NOT_COLUMNS] [-x]
              [FILE]

Filter and truncate CSV files. Like the Unix "cut" command, but for tabular
data.

positional arguments:
  FILE                  The CSV file to operate on. If omitted, will accept
                        input as piped data via STDIN.

optional arguments:
  -h, --help            show this help message and exit
  -n, --names           Display column names and indices from the input CSV
                        and exit.
  -c COLUMNS, --columns COLUMNS
                        A comma-separated list of column indices, names or
                        ranges to be extracted, e.g. "1,id,3-5". Defaults to
                        all columns.
  -C NOT_COLUMNS, --not-columns NOT_COLUMNS
                        A comma-separated list of column indices, names or
                        ranges to be excluded, e.g. "1,id,3-5". Defaults to no
                        columns. Ignores unknown columns.
  -x, --delete-empty-rows
                        After cutting, delete rows which are completely empty.

Also, see also: Arguments common to all tools β€” those shared args live in their own spot, go check 'em 🫢

NOTE 🚨 csvcut does not implement row filtering. If you want that, you gotta pipe your data to csvgrep. Periodt.
NOTE πŸ’€ If a data row is longer than the header row, its additional columns are truncated. So yeah. Extra columns just... vanish. Gone. Reduced to atoms.

πŸŽ€ EXAMPLES πŸŽ€

Wanna see the indices and names of all your columns? Say less:

$ csvcut -n examples/realdata/FY09_EDU_Recipients_by_State.csv
  1: State Name
  2: State Abbreviate
  3: Code
  4: Montgomery GI Bill-Active Duty
  5: Montgomery GI Bill- Selective Reserve
  6: Dependents' Educational Assistance
  7: Reserve Educational Assistance Program
  8: Post-Vietnam Era Veteran's Educational Assistance Program
  9: TOTAL
 10:

(yes, 10: is just sitting there with an empty name, and that's valid, we don't judge πŸ™ˆ)

Now if you only want the names and not the indices, you can chop the indices off with the cut command:

$ csvcut -n examples/realdata/FY09_EDU_Recipients_by_State.csv | cut -c6-
State Name
State Abbreviate
Code
Montgomery GI Bill-Active Duty
Montgomery GI Bill- Selective Reserve
Dependents' Educational Assistance
Reserve Educational Assistance Program
Post-Vietnam Era Veteran's Educational Assistance Program
TOTAL

Extract columns

Extract the first and third columns β€” easy:

csvcut -c 1,3 examples/realdata/FY09_EDU_Recipients_by_State.csv

Extract columns named "TOTAL" and "State Name", in that order (order matters bestie, it's giving intentional):

csvcut -c TOTAL,"State Name" examples/realdata/FY09_EDU_Recipients_by_State.csv

Extract a column that may not exist in all files β€” the iconic "will it be there? who knows" move:

echo d, | csvjoin examples/dummy.csv - | csvcut -c d
 echo d, | csvjoin examples/join_no_header_row.csv - | csvcut -c d

Other

Add line numbers to a file while making no other changes:

csvcut -l examples/realdata/FY09_EDU_Recipients_by_State.csv

Wanna display a column's unique values? Obsessed with this pipeline:

csvcut -c 1 examples/realdata/FY09_EDU_Recipients_by_State.csv | sed 1d | sort | uniq

Or, if you're feeling ✨fancy✨ and wanna do it with SQL instead:

csvcut -c 1 examples/realdata/FY09_EDU_Recipients_by_State.csv | csvsql --query 'SELECT DISTINCT("State Name") FROM stdin'

πŸ’œ AUTHOR πŸ’œ

Christopher Groskopf and contributors β€” shoutout to the whole squad 🫢


2016, Christopher Groskopf and James McKinney


and that's the whole page bestie, you ate that πŸ“

This was csvkit version 2.2.0, dated Aug 16, 2024, and honestly? iconic. bye πŸ’–βœ¨

πŸ“¦ where this page comes from