Debian Codegraph @

docs/scripts/csvgrep.rst

raw
1 =======
2 csvgrep
3 =======
4
5 Description
6 ===========
7
8 Filter tabular data to only those rows where certain columns contain a given value or match a regular expression:
9
10 .. code-block:: none
11
12 usage: csvgrep [-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 [-m PATTERN] [-r REGEX] [-f MATCHFILE] [-i] [-a]
16 [FILE]
17
18 Search CSV files. Like the Unix "grep" command, but for tabular data.
19
20 positional arguments:
21 FILE The CSV file to operate on. If omitted, will accept
22 input as piped data via STDIN.
23
24 optional arguments:
25 -h, --help show this help message and exit
26 -n, --names Display column names and indices from the input CSV
27 and exit.
28 -c COLUMNS, --columns COLUMNS
29 A comma-separated list of column indices, names or
30 ranges to be searched, e.g. "1,id,3-5".
31 -m PATTERN, --match PATTERN
32 A string to search for.
33 -r REGEX, --regex REGEX
34 A regular expression to match.
35 -f MATCHFILE, --file MATCHFILE
36 A path to a file. For each row, if any line in the
37 file (stripped of line separators) is an exact match
38 of the cell value, the row matches.
39 -i, --invert-match Select non-matching rows, instead of matching rows.
40 -a, --any-match Select rows in which any column matches, instead of
41 all columns.
42
43 See also: :doc:`../common_arguments`.
44
45 NOTE: Even though '-m', '-r', and '-f' are listed as "optional" arguments, you must specify one of them.
46
47 Examples
48 ========
49
50 Search for the row relating to Illinois:
51
52 .. code-block:: bash
53
54 csvgrep -c 1 -m ILLINOIS examples/realdata/FY09_EDU_Recipients_by_State.csv
55
56 Search for rows relating to states with names beginning with the letter "I":
57
58 .. code-block:: bash
59
60 csvgrep -c 1 -r "^I" examples/realdata/FY09_EDU_Recipients_by_State.csv
61
62 Search for rows that do not contain an empty state cell:
63
64 .. code-block:: bash
65
66 csvgrep -c 1 -r "^$" -i examples/realdata/FY09_EDU_Recipients_by_State.csv
67
68 Perform a case-insensitive search:
69
70 .. code-block:: bash
71
72 csvgrep -c 1 -r "(?i)illinois" examples/realdata/FY09_EDU_Recipients_by_State.csv
73
74 Remove comment rows:
75
76 .. code-block:: bash
77
78 printf "a,b\n1,2\n# a comment\n3,4" | csvgrep --invert-match -c1 -r '^#'
79
80 Get the indices of the columns that contain matching text (``\x1e`` is the `Record Separator (RS) character <https://en.wikipedia.org/wiki/C0_and_C1_control_codes#Field_separators>`_):
81
82 .. code-block::
83
84 csvgrep -m 22 -a -c 1- examples/realdata/FY09_EDU_Recipients_by_State.csv | csvformat -M $'\x1e' | xargs -d $'\x1e' -n1 sh -c 'echo $0 | csvcut -n' | grep 22
85
86 .. note::
87
88 This last example is not performant.