csvgrep - search for patterns in a CSV and display results in a table
csvgrep <pattern> <file> csvgrep -d <directory> <pattern>
csvgrep is a script that lets you look for a pattern in a CSV file, and then displays the results in a text table. We assume that the first line in the CSV is a header row.
The simplest usage is to look for a word in a CSV:
% csvgrep Murakami books.csv +-------------------+-----------------+-------+------+ | Book | Author | Pages | Date | +-------------------+-----------------+-------+------+ | Norwegian Wood | Haruki Murakami | 400 | 1987 | | Men without Women | Haruki Murakami | 228 | 2017 | +-------------------+-----------------+-------+------+
As with regular grep, you can use the -i switch to make it case-insensitive:
% csvgrep -i wood books.csv +---------------------+-----------------+-------+------+ | Book | Author | Pages | Date | +---------------------+-----------------+-------+------+ | Norwegian Wood | Haruki Murakami | 400 | 1987 | | A Walk in the Woods | Bill Bryson | 276 | 1997 | +---------------------+-----------------+-------+------+
You can specify a subset of the columns to display with the -c option, which takes a comma-separated list of column numbers:
% csvgrep -c 0,1,3 -i mary books.csv +--------------+--------------+------+ | Book | Author | Date | +--------------+--------------+------+ | Mary Poppins | PL Travers | 1934 | | Frankenstein | Mary Shelley | 1818 | +--------------+--------------+------+
The pattern can be a Perl regexp, but you'll probably need to quote it from your shell:
% csvgrep -i 'walk.*wood' books.csv +-----------------------+-------------+-------+------+ | Book | Author | Pages | Date | +-----------------------+-------------+-------+------+ | A Walk in the Woods | Bill Bryson | 276 | 1997 | | Death Walks the Woods | Cyril Hare | 222 | 1954 | +-----------------------+-------------+-------+------+
At work we have a number of situations where we have a directory that contains multiple versions of a particular CSV file, for example with a feed from a customer. With the -d option, csvgrep will look at the most recent file in the specified directory, only considering files with a .csv extension:
% csvgrep -d /usr/local/feeds/users -i smith
This is a script I've used internally, with features being added as I wanted them. Let me know if you've ideas for additional features, or send me a pull request.
https://github.com/neilb/csvgrep
Neil Bowers <neilb@cpan.org>
This software is copyright (c) 2017 by Neil Bowers <neilb@cpan.org>.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.
To install csvgrep, copy and paste the appropriate command in to your terminal.
cpanm
cpanm csvgrep
CPAN shell
perl -MCPAN -e shell install csvgrep
For more information on module installation, please visit the detailed CPAN module installation guide.