| 1 |
====== |
| 2 |
csvcut |
| 3 |
====== |
| 4 |
|
| 5 |
Description |
| 6 |
=========== |
| 7 |
|
| 8 |
Filters and truncates CSV files. Like the Unix "cut" command, but for tabular data: |
| 9 |
|
| 10 |
.. code-block:: none |
| 11 |
|
| 12 |
usage: csvcut [-h] [-d DELIMITER] [-t] [-q QUOTECHAR] [-u {0,1,2,3}] [-b] |
| 13 |
[-p ESCAPECHAR] [-z FIELD_SIZE_LIMIT] [-e ENCODING] [-S] [-H] |
| 14 |
[-K SKIP_LINES] [-v] [-l] [--zero] [-V] [-n] [-c COLUMNS] |
| 15 |
[-C NOT_COLUMNS] [-x] |
| 16 |
[FILE] |
| 17 |
|
| 18 |
Filter and truncate CSV files. Like the Unix "cut" command, but for tabular |
| 19 |
data. |
| 20 |
|
| 21 |
positional arguments: |
| 22 |
FILE The CSV file to operate on. If omitted, will accept |
| 23 |
input as piped data via STDIN. |
| 24 |
|
| 25 |
optional arguments: |
| 26 |
-h, --help show this help message and exit |
| 27 |
-n, --names Display column names and indices from the input CSV |
| 28 |
and exit. |
| 29 |
-c COLUMNS, --columns COLUMNS |
| 30 |
A comma-separated list of column indices, names or |
| 31 |
ranges to be extracted, e.g. "1,id,3-5". Defaults to |
| 32 |
all columns. |
| 33 |
-C NOT_COLUMNS, --not-columns NOT_COLUMNS |
| 34 |
A comma-separated list of column indices, names or |
| 35 |
ranges to be excluded, e.g. "1,id,3-5". Defaults to no |
| 36 |
columns. Ignores unknown columns. |
| 37 |
-x, --delete-empty-rows |
| 38 |
After cutting, delete rows which are completely empty. |
| 39 |
|
| 40 |
See also: :doc:`../common_arguments`. |
| 41 |
|
| 42 |
.. note:: |
| 43 |
|
| 44 |
csvcut does not implement row filtering, for this you should pipe data to :doc:`csvgrep`. |
| 45 |
|
| 46 |
.. note:: |
| 47 |
|
| 48 |
If a data row is longer than the header row, its additional columns are truncated. |
| 49 |
|
| 50 |
Examples |
| 51 |
======== |
| 52 |
|
| 53 |
Print columns |
| 54 |
------------- |
| 55 |
|
| 56 |
Print the indices and names of all columns: |
| 57 |
|
| 58 |
.. code-block:: console |
| 59 |
|
| 60 |
$ csvcut -n examples/realdata/FY09_EDU_Recipients_by_State.csv |
| 61 |
1: State Name |
| 62 |
2: State Abbreviate |
| 63 |
3: Code |
| 64 |
4: Montgomery GI Bill-Active Duty |
| 65 |
5: Montgomery GI Bill- Selective Reserve |
| 66 |
6: Dependents' Educational Assistance |
| 67 |
7: Reserve Educational Assistance Program |
| 68 |
8: Post-Vietnam Era Veteran's Educational Assistance Program |
| 69 |
9: TOTAL |
| 70 |
10: |
| 71 |
|
| 72 |
Print only the names of all columns, by removing the indices with the :code:`cut` command: |
| 73 |
|
| 74 |
.. code-block:: console |
| 75 |
|
| 76 |
$ csvcut -n examples/realdata/FY09_EDU_Recipients_by_State.csv | cut -c6- |
| 77 |
State Name |
| 78 |
State Abbreviate |
| 79 |
Code |
| 80 |
Montgomery GI Bill-Active Duty |
| 81 |
Montgomery GI Bill- Selective Reserve |
| 82 |
Dependents' Educational Assistance |
| 83 |
Reserve Educational Assistance Program |
| 84 |
Post-Vietnam Era Veteran's Educational Assistance Program |
| 85 |
TOTAL |
| 86 |
|
| 87 |
Extract columns |
| 88 |
--------------- |
| 89 |
|
| 90 |
Extract the first and third columns: |
| 91 |
|
| 92 |
.. code-block:: bash |
| 93 |
|
| 94 |
csvcut -c 1,3 examples/realdata/FY09_EDU_Recipients_by_State.csv |
| 95 |
|
| 96 |
Extract columns named "TOTAL" and "State Name" (in that order): |
| 97 |
|
| 98 |
.. code-block:: bash |
| 99 |
|
| 100 |
csvcut -c TOTAL,"State Name" examples/realdata/FY09_EDU_Recipients_by_State.csv |
| 101 |
|
| 102 |
Extract a column that may not exist in all files: |
| 103 |
|
| 104 |
.. code-block:: bash |
| 105 |
|
| 106 |
echo d, | csvjoin examples/dummy.csv - | csvcut -c d |
| 107 |
echo d, | csvjoin examples/join_no_header_row.csv - | csvcut -c d |
| 108 |
|
| 109 |
Other |
| 110 |
----- |
| 111 |
|
| 112 |
Add line numbers to a file, making no other changes: |
| 113 |
|
| 114 |
.. code-block:: bash |
| 115 |
|
| 116 |
csvcut -l examples/realdata/FY09_EDU_Recipients_by_State.csv |
| 117 |
|
| 118 |
Display a column's unique values: |
| 119 |
|
| 120 |
.. code-block:: bash |
| 121 |
|
| 122 |
csvcut -c 1 examples/realdata/FY09_EDU_Recipients_by_State.csv | sed 1d | sort | uniq |
| 123 |
|
| 124 |
Or: |
| 125 |
|
| 126 |
.. code-block:: bash |
| 127 |
|
| 128 |
csvcut -c 1 examples/realdata/FY09_EDU_Recipients_by_State.csv | csvsql --query 'SELECT DISTINCT("State Name") FROM stdin' |