从命令行查看表格文件,例如 CSV [英] View tabular file such as CSV from command line
问题描述
有人知道 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.
推荐答案
你也可以使用这个:
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屋!