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.
views:
816answers:
8Here's a (probably too) simple option:
sed "s/,/\t/g" filename.csv | less
How about one that runs inside Firefox?
http://code.google.com/p/csv-viewer/
You might also try a Google search for "csv viewer"
My FOSS project CSVfix allows you to display CSV files in "ASCII art" table format.
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>
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.
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
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.