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 CHANGELOG.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
Unreleased
----------

- fix: :doc:`/scripts/csvgrep` treats an empty :code:`--regex` as a pattern that matches every value, including with :code:`--any-match` and :code:`--invert-match`.
- 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
2 changes: 1 addition & 1 deletion csvkit/utilities/csvgrep.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ def main(self):

rows, column_names, column_ids = self.get_rows_and_column_names_and_column_ids(**reader_kwargs)

if self.args.regex:
if self.args.regex is not None:
pattern = re.compile(self.args.regex)
elif self.args.matchfile:
lines = {line.rstrip() for line in self.args.matchfile}
Expand Down
14 changes: 14 additions & 0 deletions tests/test_utilities/test_csvgrep.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,20 @@ def test_re_match(self):
['1', '2', '3'],
])

def test_empty_regex(self):
for any_match in (False, True):
for inverse in (False, True):
with self.subTest(any_match=any_match, inverse=inverse):
args = ['-c', '1,2', '-r', '', 'examples/dummy.csv']
if any_match:
args.append('--any-match')
if inverse:
args.append('--invert-match')
expected = [['a', 'b', 'c']]
if not inverse:
expected.append(['1', '2', '3'])
self.assertRows(args, expected)

def test_re_match_utf8(self):
self.assertRows(['-c', '3', '-r', 'ʤ', 'examples/test_utf8.csv'], [
['foo', 'bar', 'baz'],
Expand Down