csvgrep - grep a CSV and display results in a table

scripts Thu 22 June 2017

Prompted by PhReiuoyx's recent blog post on CSV tools, I've released csvgrep, a simple command-line script that I use at work to quickly find things in CSV files. It's simplistic, but in a lot of situations it does what I need.

Basic usage is similar to classic grep:

% csvgrep -i wood books.csv

| Book                  | Author          | Pages | Date |
| Norwegian Wood        | Haruki Murakami | 400   | 1987 |
| A Walk in the Woods   | Bill Bryson     | 276   | 1997 |
| Death Walks the Woods | Cyril Hare      | 222   | 1954 |

The pattern can be a Perl regexp (which you'll probably need to quote from your shell, and you can use the -c option to specify which columns you want:

% csvgrep -c 0,1,3 -i 'walk.*wood' books.csv

| Book                  | Author      | Date |
| A Walk in the Woods   | Bill Bryson | 1997 |
| Death Walks the Woods | Cyril Hare  | 1954 |

At work we get CSV feeds from various customers, and when there's a problem I want to search the latest version. Instead of specifying a file you can use the -d option to give a directory, and csvgrep will search the most recent .csv in there. I have a number of aliases defined to make this easy:

alias tg="csvgrep -d .../users -c 0,1,2,5,8 -i"

So then I just run:

% tg smith

In writing this I realised that it only handles one file, as that's what I've wanted to date. At some point I'll extend it to search multiple files, unless someone beats me to it with a pull request.

To install this you should just have to run one of the following:

% cpan csvgrep
% cpanm csvgrep

Update: 22nd June

So far today I've had 3 pull requests, all of which I've merged:

I've also implemented one requested feature: support for TSV files (tab separated).

Thanks to Ed Freyfogle and Scott Baker!

comments powered by Disqus