有人知道Linux / OS X的命令行CSV查看器吗?我正在考虑像less
这样的东西,但是以更易读的方式将列空格化。 (我可以使用OpenOffice Calc或Excel打开它,但是这样的方式太过于过分了,因为只需按照我需要的那样查找数据。)水平和垂直滚动会很棒。
答案 0 :(得分:380)
您也可以使用:
column -s, -t < somefile.csv | less -#2 -N -S
column
是一个非常方便的标准unix程序 - 它找到每列的适当宽度,并将文本显示为格式良好的表。
注意:只要有空字段,就需要在其中放置某种占位符,否则该列将与以下列合并。以下示例演示了如何使用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
请注意,将,,
替换为, ,
两次。如果您只执行一次,1,,,4
将变为1, ,,4
,因为已经匹配了第二个逗号。
答案 1 :(得分:90)
您可以通过
安装csvtool
(在Ubuntu上)
sudo apt-get install csvtool
然后运行:
csvtool readable filename | view -
即使你有一些非常长的值的单元格,这也会使它在一个只读的vim实例中很漂亮。
答案 2 :(得分:54)
看看csvkit。它提供了一组遵循UNIX哲学的工具(意味着它们小巧,简单,单一用途并且可以组合使用)。
以下示例从免费Maxmind World Cities database中提取德国人口最多的十个城市,并以控制台可读格式显示结果:
$ 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是独立于平台的,因为它是用Python编写的。
答案 3 :(得分:39)
Tabview:轻量级python curses命令行CSV文件查看器(以及其他表格Python数据,如列表列表)位于Github
答案 4 :(得分:28)
如果您是一名游客,请使用CSV plugin,即beautiful。{{3}}。
答案 5 :(得分:16)
可以全局安装nodejs包tecfu/tty-table以完成此操作:
apt-get install nodejs
npm i -g tty-table
cat data.csv | tty-table
它也可以处理流。
有关详细信息,请参阅docs for terminal usage here。
答案 6 :(得分:10)
我的FOSS项目CSVfix允许您以“ASCII艺术”表格格式显示CSV文件。
答案 7 :(得分:8)
Ofri的答案为您提供了所要求的一切。 但是..如果你不想记住命令,你可以将它添加到你的〜/ .bashrc(或等效的):
csview()
{
local file="$1"
sed "s/,/\t/g" "$file" | less -S
}
这与Ofri的答案完全相同,只是我将它包装在shell函数中并使用less -S
选项来停止换行(使less
更像办公室/ oocalc )。
打开一个新shell(或在当前shell中键入source ~/.bashrc
)并使用以下命令运行命令:
csview <filename>
答案 8 :(得分:8)
xsv不仅仅是一个观众。我推荐它用于命令行上的大多数CSV任务,尤其是在处理大型数据集时。
答案 9 :(得分:6)
我长期使用了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
}
它对我来说效果更好的原因是它可以更好地处理宽列。
答案 10 :(得分:4)
tblless
包装了unix column
命令,并且还对齐了数字列。
答案 11 :(得分:3)
这是一个(可能太)简单的选项:
sed "s/,/\t/g" filename.csv | less
答案 12 :(得分:2)
使用TxtSushi即可:
csvtopretty filename.csv | less -S
答案 13 :(得分:2)
我写了这个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
答案 14 :(得分:1)
Tabview真的很好。处理了200 + MB的文件,这些文件显示良好,这些文件在LibreOffice以及gvim中的csv插件中都是错误的。
Anaconda版本可在此处找到:https://anaconda.org/bioconda/tabview
答案 15 :(得分:1)
我已经为这些(和其他)目的创建了tablign。使用
安装[sudo -H] pip3 install tablign
和
$ cat test.csv
Header1,Header2,Header3
Pizza,Artichoke dip,Bob's Special of the Day
BLT,Ham on rye with the works,
$ tablign test.csv
Header1 , Header2 , Header3
Pizza , Artichoke dip , Bob's Special of the Day
BLT , Ham on rye with the works ,
如果数据由逗号以外的其他内容分隔,也可以使用。最重要的是,它保留分隔符,因此您也可以使用它来设置ASCII表的样式,而不会牺牲[Markdown,CSV,LaTeX]语法。
答案 16 :(得分:1)
又一个多功能CSV(而不仅仅是)操作工具:Miller。从它自己的描述来看,它就像awk,sed,cut,join和sort等名称索引数据,如CSV,TSV和表格JSON。 (链接到github存储库:https://github.com/johnkerl/miller)
答案 17 :(得分:0)
python中有这个简短的命令行脚本:https://github.com/rgrp/csv2ascii/blob/master/csv2ascii.py
只需下载并放入您的路径即可。用法就像
csv2ascii.py [options] csv-file-path
将csv-file-path
处的csv文件转换为返回结果的ascii表单
标准输出。如果csv-file-path
=' - '则从标准输入读取。
选项:
-h, --help show this help message and exit -w WIDTH, --width=WIDTH Width of ascii output -c COLUMNS, --columns=COLUMNS Only display this number of columns
答案 18 :(得分:0)
我为了这个目的在Groovy中编写了一个脚本viewtab。你可以调用它:
viewtab filename.csv
它基本上是一个超轻量级的电子表格,可以从命令行调用,处理CSV和制表符分隔的文件,可以读取Excel和Numbers扼流的非常大的文件,并且非常快。它不是纯文本意义上的命令行,而是独立于平台,可能适合许多寻求解决快速检查许多或大型CSV文件的人在命令行环境中工作的问题。
脚本及其安装方法如下所述:
http://bayesianconspiracy.blogspot.com/2012/06/quick-csvtab-file-viewer.html