csvcut1
Csvcut documentation
Overview
Filters and truncates CSV files. Like the Unix "cut" command, but for tabular data:
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 tabulardata.positional arguments:FILE The CSV file to operate on. If omitted, will acceptinput 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 CSVand exit.-c COLUMNS, --columns COLUMNSA comma-separated list of column indices, names orranges to be extracted, e.g. "1,id,3-5". Defaults toall columns.-C NOT_COLUMNS, --not-columns NOT_COLUMNSA comma-separated list of column indices, names orranges to be excluded, e.g. "1,id,3-5". Defaults to nocolumns. Ignores unknown columns.-x, --delete-empty-rowsAfter cutting, delete rows which are completely empty.
See also: Arguments common to all tools.
NOTE:
csvcut does not implement row filtering, for this you should pipe data to csvgrep.
NOTE:
If a data row is longer than the header row, its additional columns are truncated.
Examples
Print columns
Print the indices and names of all columns:
$ csvcut -n examples/realdata/FY09_EDU_Recipients_by_State.csv1: State Name2: State Abbreviate3: Code4: Montgomery GI Bill-Active Duty5: Montgomery GI Bill- Selective Reserve6: Dependents' Educational Assistance7: Reserve Educational Assistance Program8: Post-Vietnam Era Veteran's Educational Assistance Program9: TOTAL10:
Print only the names of all columns, by removing the indices with the cut command:
$ csvcut -n examples/realdata/FY09_EDU_Recipients_by_State.csv | cut -c6-State NameState AbbreviateCodeMontgomery GI Bill-Active DutyMontgomery GI Bill- Selective ReserveDependents' Educational AssistanceReserve Educational Assistance ProgramPost-Vietnam Era Veteran's Educational Assistance ProgramTOTAL
Extract columns
Extract the first and third columns:
csvcut -c 1,3 examples/realdata/FY09_EDU_Recipients_by_State.csv
Extract columns named "TOTAL" and "State Name" (in that order):
csvcut -c TOTAL,"State Name" examples/realdata/FY09_EDU_Recipients_by_State.csv
Extract a column that may not exist in all files:
echo d, | csvjoin examples/dummy.csv - | csvcut -c decho d, | csvjoin examples/join_no_header_row.csv - | csvcut -c d
Other
Add line numbers to a file, making no other changes:
csvcut -l examples/realdata/FY09_EDU_Recipients_by_State.csv
Display a column's unique values:
csvcut -c 1 examples/realdata/FY09_EDU_Recipients_by_State.csv | sed 1d | sort | uniq
Or:
csvcut -c 1 examples/realdata/FY09_EDU_Recipients_by_State.csv | csvsql --query 'SELECT DISTINCT("State Name") FROM stdin'
Releases
4 releases since Debian 11 (Bullseye); the text changed 3 times.
--columns until Debian 12 (Bookworm);
--delete-empty-rows until Debian 12 (Bookworm);
--delimiter until Debian 12 (Bookworm);
--encoding until Debian 12 (Bookworm);
--escapechar until Debian 12 (Bookworm);
--help until Debian 12 (Bookworm);
--linenumbers until Debian 12 (Bookworm);
--maxfieldsize until Debian 12 (Bookworm);
--names until Debian 12 (Bookworm);
--no-doublequote until Debian 12 (Bookworm);
--no-header-row until Debian 12 (Bookworm);
--not-columns until Debian 12 (Bookworm);
--quotechar until Debian 12 (Bookworm);
--quoting until Debian 12 (Bookworm);
--skip-lines until Debian 12 (Bookworm);
--skipinitialspace until Debian 12 (Bookworm);
--tabs until Debian 12 (Bookworm);
--verbose until Debian 12 (Bookworm);
--version until Debian 12 (Bookworm);
--zero until Debian 12 (Bookworm);
-C until Debian 12 (Bookworm);
-H until Debian 12 (Bookworm);
-K until Debian 12 (Bookworm);
-S until Debian 12 (Bookworm);
-V until Debian 12 (Bookworm);
-b until Debian 12 (Bookworm);
-c until Debian 12 (Bookworm);
-d until Debian 12 (Bookworm);
-e until Debian 12 (Bookworm);
-h until Debian 12 (Bookworm);
-l until Debian 12 (Bookworm);
-n until Debian 12 (Bookworm);
-p until Debian 12 (Bookworm);
-q until Debian 12 (Bookworm);
-t until Debian 12 (Bookworm);
-u until Debian 12 (Bookworm);
-v until Debian 12 (Bookworm);
-x until Debian 12 (Bookworm);
-z until Debian 12 (Bookworm).