views:

816

answers:

8

Anyone know of a command-line CSV viewer for Linux/OS X? I'm thinking of something like less but that spaces out the columns in a more readable way. (I'd be fine with opening it with OpenOffice Calc or Excel, but that's way too overpowered for just looking at the data like I need to.) Having horizontal and vertical scrolling would be great.

+1  A: 

Here's a (probably too) simple option:

sed "s/,/\t/g" filename.csv | less
Ofri Raviv
woo I came here to say something similar!
pisswillis
That was my first inclination as well. But you have to insert enough tabs to match the longest value for your column... Started getting a little complicated and I thought "someone else must have done this already."
Benjamin Oakes
+1  A: 

How about one that runs inside Firefox?

http://code.google.com/p/csv-viewer/

You might also try a Google search for "csv viewer"

Jim Garrison
+6  A: 

My FOSS project CSVfix allows you to display CSV files in "ASCII art" table format.

anon
Exactly what I was looking for. I'll have to try compiling it for OS X. (You might have some patches coming your way, who knows...)
Benjamin Oakes
I'd very much welcome them. One opf the slightly depressing aspects of FOSS projects is how few people actually contribute code. Of course, I'm as guilty of this as the next person.
anon
Why does your CSVfix project not allow repo browsing? Making it harder for others to see the code does not make it more likely that you get contributions, does it?
Dirk Eddelbuettel
Sheer laziness on my part, I'm afraid. Also, providing a zip of the code means anyone can get it - if I only provided repo access, people would have to have SVM or Hg installed. If I started receiving patches, I'd reconsider.
anon
A: 

Ofri's answer gives you everything you asked for. But.. if you don't want to remember the command you can add this to your ~/.bashrc (or equivalent):

csview()
{
local file="$1"
sed "s/,/\t/g" "$file" | less -S
}

This is exactly the same as Ofri's answer except I have wrapped it in a shell function and am using the less -S option to stop the wrapping of lines (makes less behaves more like a office/oocalc).

Open a new shell (or type source ~/.bashrc in your current shell) and run the command using:

csview <filename>

pisswillis
+2  A: 

You can install csvtools (it's in the Ubuntu repository), and run

cvstools 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.

d_chall
+1  A: 

Using TxtSushi you can do:

csvtopretty filename.csv | less -S

Keith
+1  A: 

I wrote this csv_view.sh to format CSVs from the command line, this reads the entire file to figure out the optimal width of each column (requires perl, assumes there are no commas in fields, also uses less):


#!/bin/bash

perl -we '
  sub max( @ ) {
    my $max = shift;

    map { $max = $_ if $_ > $max } @_;
    return $max;
  }

  sub transpose( @ ) {
    my @matrix = @_;
    my $width  = scalar @{ $matrix[ 0 ] };
    my $height = scalar @matrix;

    return map { my $x = $_; [ map { $matrix[ $_ ][ $x ] } 0 .. $height - 1 ] } 0 .. $width - 1;
  }

  # Read all lines, as arrays of fields
  my @lines = map { s/\r?\n$//; [ split /,/ ] } ;

  my $widths =
    # Build a pack expression based on column lengths
    join "",

    # For each column get the longest length plus 1
    map { 'A' . ( 1 + max map { length } @$_ ) }

    # Get arrays of columns
    transpose

    @lines
  ;

  # Format all lines with pack
  map { print pack( $widths, @$_ ) . "\n" } @lines;
' $1 | less -NS

Jean Vincent
+3  A: 

You can also use this::

cat somefile.csv | column -s, -t | 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.

Cool -- I'll check that out.
Benjamin Oakes