如何在Linux命令行上解析CSV文件? [英] How can I parse CSV files on the Linux command line?
问题描述
如何在Linux命令行上解析CSV文件?
How can I parse CSV files on the Linux command line?
要做类似的事情:
csvparse -c 2,5,6 filename
从所有行的第2、5和6列中提取字段.
to extract fields from columns 2, 5 and 6 from all rows.
它应该能够处理csv文件格式: http://tools.ietf.org/html /rfc4180 表示引用字段并适当转义内部引号,因此对于具有3个字段的示例行:
It should be able to handle the csv file format: http://tools.ietf.org/html/rfc4180 which means quoting fields and escaping inner quotes as appropriate, so for an example row with 3 fields:
field1,"field, number ""2"", has inner quotes and a comma",field3
这样,如果我在上面的行中请求字段2,我会得到:
so that if I request field 2 for the row above I get:
field, number "2", has inner quotes and a comma
我很高兴有很多解决方案,例如Perl,Awk(等等),但是我想要一个本机bash命令行工具,它不需要我调用其他脚本环境或编写任何其他代码(!).
I appreciate that there are numerous solutions, Perl, Awk (etc.) to this problem but I would like a native bash command line tool that does not require me to invoke some other scripting environment or write any additional code(!).
推荐答案
我的FOSS CSV流编辑器 CSVfix 完全可以满足您的需求.有一个针对Windows的二进制安装程序,以及一个针对UNIX/Linux的可编译版本(通过makefile).
My FOSS CSV stream editor CSVfix does exactly what you want. There is a binary installer for Windows, and a compilable version (via a makefile) for UNIX/Linux.
这篇关于如何在Linux命令行上解析CSV文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!