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

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


You can also use this:

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

column is a standard unix program that is very convenient -- it finds the appropriate width of each column, and displays the text as a nicely formatted table.

Note: whenever you have empty fields, you need to put some kind of placeholder in it, otherwise the column gets merged with following columns. The following example demonstrates how to use sed to insert a placeholder:

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

Note that the substitution of ,, for , , is done twice. If you do it only once, 1,,,4 will become 1, ,,4 since the second comma is matched already.


You can install csvtool (on Ubuntu) via

sudo apt-get install csvtool

and then run:

csvtool readable filename | view -

This will make it nice and pretty inside of a read-only vim instance, even if you have some cells with very long values.


Have a look at csvkit. It provides a set of tools that adhere to the UNIX philosophy (meaning they are small, simple, single-purposed and can be combined).

Here is an example that extracts the ten most populated cities in Germany from the free Maxmind World Cities database and displays the result 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      |-----------------------------------------------------

Csvkit is platform independent because it is written in Python.