有人知道Linux/OS X的命令行CSV查看器吗?我在考虑一些更少的东西,但以一种更可读的方式分隔列。(我可以用OpenOffice Calc或Excel打开它,但这对于我需要查看的数据来说太强大了。)有水平和垂直滚动会很棒。
当前回答
我为这些(和其他)目的创建了一个表。安装与
pip install tablign
and
$ 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]语法。
其他回答
我写了这个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
为此,我在Groovy中编写了一个脚本viewtab。你可以这样调用它:
viewtab filename.csv
它基本上是一个超轻量级的电子表格,可以从命令行调用,处理CSV和制表符分离的文件,可以读取Excel和Numbers阻塞的非常大的文件,并且非常快。从纯文本的意义上讲,它不是命令行,但它是独立于平台的,可能适合许多人在命令行环境中寻找快速检查许多或大型CSV文件的解决方案。
脚本和如何安装它描述在这里:
http://bayesianconspiracy.blogspot.com/2012/06/quick-csvtab-file-viewer.html
我用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
}
对我来说,它工作得更好的原因是它能更好地处理宽列。
我为这些(和其他)目的创建了一个表。安装与
pip install tablign
and
$ 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]语法。
还有另一个多功能的CSV(不仅仅是)操作工具:Miller。从它自己的描述来看,它类似于名称索引数据(如CSV、TSV和表格JSON)的awk、sed、cut、join和sort。(链接到github仓库:https://github.com/johnkerl/miller)
推荐文章
- 在SSH会话中查找客户端的IP地址
- C++ Linux的想法?
- 在OSX 10.11中安装Scrapy时,“OSError: [Errno 1]操作不允许”(El Capitan)(系统完整性保护)
- 用csv模块从csv文件中读取特定的列?
- 如何为Fedora安装g++ ?
- 如何在Mac OS X 10.6中使硬件发出哔哔声
- 如何触发命令行PHP脚本的XDebug分析器?
- 从Cocoa应用程序执行一个终端命令
- Android Studio无法找到有效的Jvm(与MAC OS相关)
- Linux删除大小为0的文件
- NSRange从Swift Range?
- Maven命令行如何指向特定的settings.xml为单个命令?
- Spring引导应用程序作为服务
- 如何重定向标准derr和标准输出到不同的文件在同一行脚本?
- Windows和Linux上的c++编译:ifdef开关