linux - 如何在 Linux 命令行上解析 CSV 文件?

如何在 Linux 命令行上解析 CSV 文件?

执行以下操作:

csvparse -c 2,5,6 filename

从所有行的第 2、5 和 6 列中提取字段。

应该可以处理csv文件格式:https://www.rfc-editor.org/rfc/rfc4180这意味着引用字段并根据需要转义内部引号,因此对于具有 3 个字段的示例行:

field1,"field, number ""2"", has inner quotes and a comma",field3

所以如果我为上面的行请求字段 2,我会得到:

field, number "2", has inner quotes and a comma

我很欣赏这个问题有很多解决方案,Perl、Awk(等),但我想要一个不需要我调用其他脚本环境或编写任何额外代码的 native bash 命令行工具(!) .

最佳答案

csvtool 真的很好。在 Debian/Ubuntu (apt-get install csvtool) 中可用。示例:

csvtool namedcol Account,Cost input.csv > output.csv

见 CSVTool manual page使用提示。

https://stackoverflow.com/questions/1063125/

相关文章:

php - 根据外部请求在 Netbeans 中启动 XDebug

linux - 是否有一个 linux 命令来确定与给定进程 ID 关联的窗口 ID?

linux - 永久反转补丁文件

c++ - 我在哪里放置第三方库来设置 C++ Linux 开发环境?

linux - 如果关键字触发然后执行命令,Shell 脚本来监视日志文件?

python - 如何在 Python 中监听 Linux 中的 'usb device inser

linux - 在像 "f () {}"这样的 bash shell 脚本函数定义中使用的括号是什么

c++ - 如何在 Linux 中找到 'temp' 目录?

php - 脚本 "/home/...../public_html/index.php"的 UID

windows - Node.js 开发,windows 还是 linux?