Linux 命令行 CSV 查看器?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1875305/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 17:57:41  来源:igfitidea点击:

Command line CSV viewer?

linuxmacoscommand-linecsv

提问by Benjamin Oakes

Anyone know of a command-line CSV viewer for Linux/OS X? I'm thinking of something like lessbut 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 lookingat the data like I need to.) Having horizontal and vertical scrolling would be great.

有人知道 Linux/OS X 的命令行 CSV 查看器吗?我正在考虑类似的事情,less但是以更易读的方式将列隔开。(我可以用 OpenOffice Calc 或 Excel 打开它,但对于像我需要的那样只查看数据来说这太强大了。)水平和垂直滚动会很棒。

采纳答案by user437522

You can also use this:

你也可以使用这个:

column -s, -t < somefile.csv | less -#2 -N -S

columnis 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.

column是一个非常方便的标准 unix 程序——它找到每列的适当宽度,并将文本显示为格式良好的表格。

Note: whenever you have empty fields, you need to put some kind of placeholder in it, otherwise the column gets merged with following columns. The following example demonstrates how to use sedto insert a placeholder:

注意:每当您有空字段时,您都需要在其中放置某种占位符,否则该列将与以下列合并。以下示例演示了如何使用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

Note that the substitution of ,,for , ,is done twice. If you do it only once, 1,,,4will become 1, ,,4since the second comma is matched already.

请注意,,,for的替换进行, ,了两次。如果你只做一次,1,,,4将会变成,1, ,,4因为第二个逗号已经匹配了。

回答by Ofri Raviv

Here's a (probably too) simple option:

这是一个(可能也是)简单的选项:

sed "s/,/\t/g" filename.csv | less

回答by Ofri Raviv

My FOSS project CSVfixallows you to display CSV files in "ASCII art" table format.

我的 FOSS 项目CSVfix允许您以“ASCII 艺术”表格格式显示 CSV 文件。

回答by pisswillis

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):

Ofri 的回答为您提供了您所要求的一切。但是..如果你不想记住命令,你可以将它添加到你的 ~/.bashrc (或等效的):

csview()
{
local file=""
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 -Soption to stop the wrapping of lines (makes lessbehaves more like a office/oocalc).

这与 Ofri 的答案完全相同,除了我将它包装在一个 shell 函数中并且我使用了less -S停止换行的选项(使less行为更像办公室/oocalc)。

Open a new shell (or type source ~/.bashrcin your current shell) and run the command using:

打开一个新的 shell(或输入source ~/.bashrc您当前的 shell)并使用以下命令运行命令:

csview <filename>

csview <filename>

回答by d_chall

You can install csvtool(on Ubuntu) via

您可以csvtool通过以下方式安装(在 Ubuntu 上)

sudo apt-get install csvtool

and then run:

然后运行:

csvtool 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.

这将使它在只读 vim 实例中变得漂亮漂亮,即使您有一些具有很长值的单元格。

回答by Keith

Using TxtSushiyou can do:

使用TxtSushi你可以:

csvtopretty filename.csv | less -S

回答by Jean Vincent

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):

我写了这个 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;
'  | less -NS

回答by P. Myer Nore

If you're a vimmer, use the CSV plugin, which is juuust beautiful.

如果您是 vimmer,请使用CSV 插件,它非常漂亮

回答by Kai Sternad

Have a look at csvkit. It provides a set of tools that adhere to the UNIX philosophy (meaning they are small, simple, single-purposed and can be combined).

看看csvkit。它提供了一组遵循 UNIX 哲学的工具(意味着它们小、简单、用途单一且可以组合)。

Here is an example that extracts the ten most populated cities in Germany from the free Maxmind World Cities databaseand displays the result in a console-readable format:

以下是从免费的Maxmind World Cities 数据库中提取德国人口最多的十个城市并以控制台可读格式显示结果的示例

$ 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 is platform independent because it is written in Python.

Csvkit 是平台无关的,因为它是用 Python 编写的。

回答by James Durbin

I wrote a script, viewtab, in Groovy for just this purpose. You invoke it like:

为此,我在 Groovy 中编写了一个脚本viewtab。你像这样调用它:

viewtab filename.csv

It is basically a super-lightweight spreadsheet that can be invoked from the command line, handles CSV and tab separated files, can read VERY large files that Excel and Numbers choke on, and is very fast. It's not command-line in the sense of being text-only, but it is platform independent and will probably fit the bill for many people looking for a solution to the problem of quickly inspecting many or large CSV files while working in a command line environment.

它基本上是一个超轻量级的电子表格,可以从命令行调用,处理 CSV 和制表符分隔的文件,可以读取 Excel 和 Numbers 阻塞的非常大的文件,并且速度非常快。它不是纯文本意义上的命令行,但它是独立于平台的,可能适合许多寻求解决在命令行环境中工作时快速检查许多或大型 CSV 文件的问题的人的要求.

The script and how to install it are described here:

此处描述了脚本以及如何安装它:

http://bayesianconspiracy.blogspot.com/2012/06/quick-csvtab-file-viewer.html

http://bayesianconspiracy.blogspot.com/2012/06/quick-csvtab-file-viewer.html