有人知道Linux/OS X的命令行CSV查看器吗?我在考虑一些更少的东西,但以一种更可读的方式分隔列。(我可以用OpenOffice Calc或Excel打开它,但这对于我需要查看的数据来说太强大了。)有水平和垂直滚动会很棒。
当前回答
在python中有一个简短的命令行脚本:https://github.com/rgrp/csv2ascii/blob/master/csv2ascii.py
只需下载并放置在您的路径。用法就像
csv2ascii.py [options] csv-file-path
转换csv文件在csv-file-path到ascii形式返回的结果 stdout。如果csv-file-path = '-'则从stdin读取。
选项:
-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
其他回答
在python中有一个简短的命令行脚本:https://github.com/rgrp/csv2ascii/blob/master/csv2ascii.py
只需下载并放置在您的路径。用法就像
csv2ascii.py [options] csv-file-path
转换csv文件在csv-file-path到ascii形式返回的结果 stdout。如果csv-file-path = '-'则从stdin读取。
选项:
-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
我写了这个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
这里有一个(可能太)简单的选择:
sed "s/,/\t/g" filename.csv | less
看看csvkit。它提供了一组遵循UNIX哲学的工具(这意味着它们很小、简单、用途单一,并且可以组合使用)。
下面是一个例子,它从免费的Maxmind世界城市数据库中提取了德国人口最多的十个城市,并以控制台可读的格式显示结果:
$ 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编写的。
Tabview:轻量级的python curses命令行CSV文件查看器(以及其他表格式python数据,如列表中的列表)在Github上
特点:
Python 2.7+, 3.x Unicode support Spreadsheet-like view for easily visualizing tabular data Vim-like navigation (h,j,k,l, g(top), G(bottom), 12G goto line 12, m - mark, ' - goto mark, etc.) Toggle persistent header row Dynamically resize column widths and gap Sort ascending or descending by any column. 'Natural' order sort for numeric values. Full-text search, n and p to cycle between search results 'Enter' to view the full cell contents Yank cell contents to clipboard F1 or ? for keybindings Can also use from python command line to visualize any tabular data (e.g. list-of-lists)
推荐文章
- 如何从终端/命令行调用VS代码编辑器
- 访问限制:'Application'类型不是API(必需库rt.jar的限制)
- 为什么在Mac OS X v10.9 (Mavericks)的终端中apt-get功能不起作用?
- 在Bash中检查变量是否存在于列表中
- 在Jar文件中运行类
- 查看PS命令的全部输出
- 如何在Windows命令提示符下运行.sh ?
- “你有邮件”的消息在终端,os X
- Linux命令将域名转换为IP
- 如何从命令行通过mysql运行一个查询?
- Mac OS X中的环境变量
- 如何从命令行在windows中找到mysql数据目录
- 如何从命令行安装cygwin组件?
- 如何更改Git日志日期格式
- 如何从macOS完全卸载蟒蛇