Perl 脚本(或任何东西)来合计 CSV 列
我(在其他人的大量帮助下)写了一个 awk
命令 对 CSV 文件中的列进行总计。不幸的是,我在谷歌搜索后了解到,awk
并不擅长处理 CSV 文件,因为分隔符并不总是相同(即逗号在引号引起来时应该被忽略)。
看起来也许 Perl 脚本可以做得更好。是否有可能有一个一行 Perl 脚本(或几乎同样简洁的脚本)来实现与此 awk
命令相同的功能,该命令总计 CSV 文件的第 5 列?
cat file.csv | awk -F "\"*,\"*" '{s+=$5} END {printf("%01.2f\n", s)}'
我并不是特别喜欢 Perl,但我希望避免编写成熟的 PHP 脚本。此时我已经可以轻松编写 PHP 脚本了,但现在我已经走到这一步了,我想看看我是否可以遵循它。
I wrote (with lots of help from others) an awk
command to total up a column in a CSV file. Unfortunately, I learned after some Googling that awk
isn't great at handling CSV files due to the fact that the separator is not always the same (i.e. commas should be ignored when surround by quotes).
It seems that perhaps a Perl script could do better. Would it be possible to have a one-line Perl script (or something nearly as succinct) that achieves the same thing as this awk
command that totals up the 5th column of a CSV file?
cat file.csv | awk -F "\"*,\"*" '{s+=$5} END {printf("%01.2f\n", s)}'
I'm not married to Perl in particular but I was hoping to avoid writing a full-blown PHP script. By this time I could have easily written a PHP script, but now that I've come this far, I want to see if I can follow it through.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您需要使用一个像样的 CSV 解析器来处理 CSV 格式的所有复杂性。 Text::CSV_XS (或 Text::CSV(如果不可用)是首选之一。
这是实际的 Perl 代码,为了更好的可读性
可以通过使用质量稍差但更密集的 Perl 来缩短上面的内容:
You need to use a decent CSV parser to deal with all the complexities of CSV format. Text::CSV_XS (or Text::CSV if that's not avialable) is one of the preferred ones.
Here's the actual Perl code, for better readability
The above could be shortened by using slightly lesser quality but denser Perl:
您反对使用 Perl 模块吗?您可以使用 Text::CSV 轻松完成此操作,而无需使用自己的解析器。
教程代码段更改为执行总计:
Are you opposed to using a Perl module? You can use Text::CSV to do this easily without rolling your own parser.
Tutorial snippet changed to perform total:
Python
不是一句简单的话,但相当简洁。
Python
Not a one-liner, but pretty terse.
有很多工具可以做到这一点。快速搜索“cli csvparser”让我找到了几个工具(我显然无法链接到这些工具——可能是为了防止垃圾邮件)。
我安装了我找到的第一个工具 - csvtool - 并且能够执行与您类似的命令行并获得总计。
There are a number of tools that do this. A quick search for 'cli csvparser' lead me to several tools (which I apparently can't link to--possibly to prevent spamming).
I installed the first one I found--csvtool--and was able to do a similar command line as yours and get a total.
非常短(且快速)的解决方案:
Pretty short (and fast) solution: