从命令行查看表格文件,例如CSV [英] View tabular file such as CSV from command line

查看:69
本文介绍了从命令行查看表格文件,例如CSV的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人都知道Linux/OS X的命令行CSV查看器吗?我正在考虑类似 less 的方法,但是以一种更易于理解的方式将列隔开.(我可以用OpenOffice Calc或Excel打开它,但这样做太强大了,以至于查看像我需要的数据一样.)进行水平和垂直滚动会很棒.

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

推荐答案

您还可以使用以下方法:

You can also use this:

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

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

column is 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.

注意:每当您有空字段时,都需要在其中放置某种占位符,否则该列将与后面的列合并.下面的示例演示如何使用 sed 插入占位符:

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 sed to insert a placeholder:

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

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

这篇关于从命令行查看表格文件,例如CSV的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆