有人知道Linux/OS X的命令行CSV查看器吗?我在考虑一些更少的东西,但以一种更可读的方式分隔列。(我可以用OpenOffice Calc或Excel打开它,但这对于我需要查看的数据来说太强大了。)有水平和垂直滚动会很棒。


当前回答

Tabulator包中的tblless包装了unix column命令,并且还对数值列进行对齐。

其他回答

我用pisswillis的答案用了很长时间。

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

但后来结合了一些我在http://chrisjean.com/2011/06/17/view-csv-data-from-the-command-line上找到的更适合我的代码:

csview()
{
    local file="$1"
    cat "$file" | sed -e 's/,,/, ,/g' | column -s, -t | less -#5 -N -S
}

对我来说,它工作得更好的原因是它能更好地处理宽列。

我写了这个csv_view.sh来从命令行格式化csv,这读取整个文件来找出每列的最佳宽度(需要perl,假设字段中没有逗号,也使用较少):


#!/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

如果你是一个vimmer,使用CSV插件,它非常漂亮:

.

使用TxtSushi您可以做到:

csvtopretty filename.csv | less -S

可以在全局安装nodejs包tecfu/tty-table,这样做:

apt-get install nodejs
npm i -g tty-table
cat data.csv | tty-table

它还可以处理流。

要了解更多信息,请参阅终端使用的文档。