Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions AUTHORS.rst
Original file line number Diff line number Diff line change
Expand Up @@ -116,3 +116,4 @@ The following individuals have contributed code to csvkit:
* lamdevhs
* Sai Asish Y
* Peng-Yu Chen
* Chris
1 change: 1 addition & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
Unreleased
----------

- feat: :doc:`/scripts/csvgrep` adds an :code:`--ignore-case` option to match case-insensitively, for the :code:`-m`, :code:`-r` and :code:`-f` options.
- feat: :doc:`/scripts/csvcut` adds an :code:`--ignore-unknown-columns` option to skip identifiers in :code:`-c/--columns` that do not match a column in the input.
- feat: :doc:`/scripts/csvclean` adds a :code:`--remove-empty-columns` option to remove empty columns from standard output.
- feat: :doc:`/scripts/in2csv` guesses the ``ndjson`` format for files with :code:`.ndjson`, :code:`.jsonl` and :code:`.jl` extensions.
Expand Down
18 changes: 16 additions & 2 deletions csvkit/utilities/csvgrep.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ def add_arguments(self):
self.argparser.add_argument(
'-a', '--any-match', dest='any_match', action='store_true',
help='Select rows in which any column matches, instead of all columns.')
self.argparser.add_argument(
'--ignore-case', dest='ignore_case', action='store_true',
help='Ignore case when matching, for the -m, -r and -f options.')

def main(self):
if self.args.names_only:
Expand All @@ -61,13 +64,24 @@ def main(self):
rows, column_names, column_ids = self.get_rows_and_column_names_and_column_ids(**reader_kwargs)

if self.args.regex:
pattern = re.compile(self.args.regex)
pattern = re.compile(self.args.regex, re.IGNORECASE if self.args.ignore_case else 0)
elif self.args.matchfile:
lines = {line.rstrip() for line in self.args.matchfile}
self.args.matchfile.close()

if self.args.ignore_case:
lines = {line.lower() for line in lines}

def pattern(x):
return x.lower() in lines
else:
def pattern(x):
return x in lines
elif self.args.ignore_case:
needle = self.args.pattern.lower()

def pattern(x):
return x in lines
return needle in x.lower()
else:
pattern = self.args.pattern

Expand Down
10 changes: 9 additions & 1 deletion docs/scripts/csvgrep.rst
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ Filter tabular data to only those rows where certain columns contain a given val
[-p ESCAPECHAR] [-z FIELD_SIZE_LIMIT] [-e ENCODING] [-S] [-H]
[-K SKIP_LINES] [-v] [-l] [--zero] [-V] [-n] [-c COLUMNS]
[-m PATTERN] [-r REGEX] [-f MATCHFILE] [-i] [-a]
[FILE]
[--ignore-case] [FILE]

Search CSV files. Like the Unix "grep" command, but for tabular data.

Expand All @@ -39,6 +39,8 @@ Filter tabular data to only those rows where certain columns contain a given val
-i, --invert-match Select non-matching rows, instead of matching rows.
-a, --any-match Select rows in which any column matches, instead of
all columns.
--ignore-case Ignore case when matching, for the -m, -r and -f
options.

See also: :doc:`../common_arguments`.

Expand Down Expand Up @@ -67,6 +69,12 @@ Search for rows that do not contain an empty state cell:

Perform a case-insensitive search:

.. code-block:: bash

csvgrep -c 1 --ignore-case -m illinois examples/realdata/FY09_EDU_Recipients_by_State.csv

The ``--ignore-case`` option applies to the ``-m``, ``-r`` and ``-f`` options. Alternatively, an inline flag can be used in a regular expression:

.. code-block:: bash

csvgrep -c 1 -r "(?i)illinois" examples/realdata/FY09_EDU_Recipients_by_State.csv
Expand Down
1 change: 1 addition & 0 deletions examples/test_ignore_case_matchfile.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
illinois
39 changes: 39 additions & 0 deletions tests/test_utilities/test_csvgrep.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,45 @@ def test_string_match(self):
['ILLINOIS', 'IL', '17', '15,659', '2,491', '2,025', '1,770', '19', '21,964', ''],
])

def test_match_ignore_case(self):
self.assertRows(['-c', '1', '-m', 'illinois', '--ignore-case',
'examples/realdata/FY09_EDU_Recipients_by_State.csv'], [
['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', ''],
['ILLINOIS', 'IL', '17', '15,659', '2,491', '2,025', '1,770', '19', '21,964', ''],
])

def test_match_case_sensitive_by_default(self):
self.assertRows(['-c', '1', '-m', 'illinois',
'examples/realdata/FY09_EDU_Recipients_by_State.csv'], [
['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', ''],
])

def test_re_match_ignore_case(self):
self.assertRows(['-c', '1', '-r', '^illinois$', '--ignore-case',
'examples/realdata/FY09_EDU_Recipients_by_State.csv'], [
['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', ''],
['ILLINOIS', 'IL', '17', '15,659', '2,491', '2,025', '1,770', '19', '21,964', ''],
])

def test_file_match_ignore_case(self):
self.assertRows(['-c', '1', '-f', 'examples/test_ignore_case_matchfile.txt', '--ignore-case',
'examples/realdata/FY09_EDU_Recipients_by_State.csv'], [
['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', ''],
['ILLINOIS', 'IL', '17', '15,659', '2,491', '2,025', '1,770', '19', '21,964', ''],
])

def test_match_with_line_numbers(self):
self.assertRows(['-c', '1', '-m', 'ILLINOIS', '--linenumbers',
'examples/realdata/FY09_EDU_Recipients_by_State.csv'], [
Expand Down