Coder Perfect

View tabular file such as CSV from command line [closed]

Problem

Anyone know of a Linux/OS X command-line CSV viewer? I’m thinking of something similar to less, but with the columns spaced out more readable. (I could open it in OpenOffice Calc or Excel, but that’s way too powerful for merely glancing at the data the way I need to.) It would be fantastic if you could scroll horizontally and vertically.

Asked by Benjamin Oakes

Solution #1

You can also use the following:

column -s, -t < somefile.csv | less -#2 -N -S

column is a useful standard Unix programme that determines the appropriate column width and displays the text as a nicely styled table.

Note: If you have any empty fields, you must use a placeholder; otherwise, the column will be combined with the next columns. The following example shows how to insert a placeholder with sed:

$ cat data.csv
1,2,3,4,5
1,,,,5
$ sed 's/,,/, ,/g;s/,,/, ,/g' data.csv | column -s, -t
1  2  3  4  5
1           5
$ cat data.csv
1,2,3,4,5
1,,,,5
$ column -s, -t < data.csv
1  2  3  4  5
1  5
$ sed 's/,,/, ,/g;s/,,/, ,/g' data.csv | column -s, -t
1  2  3  4  5
1           5

It’s worth noting that the substitution of,, for,, occurs twice. If you do it only once, 1,,,4 will become 1, ,,4 since the second comma is matched already.

Answered by user437522

Solution #2

Installing csvtool (on Ubuntu) is simple.

sudo apt-get install csvtool

and then run:

csvtool readable filename | view -

Even if you have certain cells with really long values, this will make it seem great in a read-only vim instance.

Answered by d_chall

Solution #3

Take a peek at the csvkit website. It gives you a set of tools that follow the UNIX concept (meaning they are small, simple, single-purposed and can be combined).

Here’s an example that uses the free Maxmind World Cities database to extract the 10 most inhabited cities in Germany and displays the results in a console-readable format:

$ csvgrep -e iso-8859-1 -c 1 -m "de" worldcitiespop | csvgrep -c 5 -r "\d+" 
  | csvsort -r -c 5 -l | csvcut -c 1,2,4,6 | head -n 11 | csvlook
-----------------------------------------------------
|  line_number | Country | AccentCity | Population  |
-----------------------------------------------------
|  1           | de      | Berlin     | 3398362     |
|  2           | de      | Hamburg    | 1733846     |
|  3           | de      | Munich     | 1246133     |
|  4           | de      | Cologne    | 968823      |
|  5           | de      | Frankfurt  | 648034      |
|  6           | de      | Dortmund   | 594255      |
|  7           | de      | Stuttgart  | 591688      |
|  8           | de      | Düsseldorf | 577139      |
|  9           | de      | Essen      | 576914      |
|  10          | de      | Bremen     | 546429      |
-----------------------------------------------------

Because it is written in Python, Csvkit is platform agnostic.

Answered by Kai Sternad

Solution #4

On Github, you can find Tabview: a lightweight python curses command line CSV file viewer (as well as other tabular Python data, such as a list of lists).

Answered by Scott Hansen

Solution #5

If you’re a vimmer, the CSV plugin is very stunning.

Answered by Myer

Post is based on https://stackoverflow.com/questions/1875305/view-tabular-file-such-as-csv-from-command-line