Perl脚本(或其它),至总量达CSV列 [英] Perl script (or anything) to total up CSV column

查看:103
本文介绍了Perl脚本(或其它),至总量达CSV列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写的(有很多别人的帮助)的<一个href=\"http://stackoverflow.com/questions/4159224/excel-and-awk-disagree-about-csv-totals/4159404#4159404\"><$c$c>awk命令到CSV文件总量可达一列。不幸的是,我的一些谷歌搜索后得知, AWK 不在处理的CSV文件很大,由于事实,即分隔符并不总是相同(即逗号应环绕时忽略用引号)。

看来,也许一个Perl脚本,可以做的更好。有没有可能有一个单行Perl脚本(或东西几乎一样简洁)的实现同样的事情,因为这 AWK 的命令,总计一个CSV的第5列文件?

 猫FILE.CSV | awk的-F\\* \\*'{S + = $ 5} END {printf的(%01.2f \\ n,S)}

我没有结婚到Perl的特别,但我希望避免编写一个全面的PHP脚本。到了这个时候,我可以很容易地编写PHP脚本,但现在,我已经走到这一步,我想看看我是否能跟随它通过。


解决方案

您需要使用一个体面的CSV解析器来处理CSV格式的所有复杂。 文本:: CSV_XS (或的文字:: CSV 如果这不是可用的编缉)是preferred的一个。

 的perl -e'{使用文字:: CSV_XS;我的$ = CSV文本:: CSV_XS-&gt;新建();打开我的$跳频,&LT;,FILE.CSV或死亡:;FILE.CSV $!我的$总和= 0;而(我的$行= $ csv-&GT;函数getline($ FH)){$总和+ = $行向&GT; [4]};关闭$ FH;打印$总和\\ n;}'

下面是实际的Perl code,为更好的可读性

 使用文字:: CSV_XS; #使用解析器库
我的$ = CSV文本:: CSV_XS-&gt;新建(); #创建解析器对象
打开我的$跳频,&LT;,FILE.CSV或死亡:;FILE.CSV $! #打开文件。
我的$总和= 0;
而(我的$行= $ csv-&GT;函数getline($ FH)){#$行字段值的数组现在
    $总和+ = $行级别&GT; [4];
}
关闭$ FH;
打印$总和\\ N的;


以上可以使用质量稍小,但密集的Perl 缩短

 猫FILE.CSV | perl的-mtext :: CSV_XS -nae'$ CSV =文本:: CSV_XS-&gt;新建();
               $ csv-&GT;解析($ _); @ F = $ csv-&GT;字段(); $ S + = $ F [4]} {打印$ S \\ N'

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.

解决方案

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.

perl -e '{use Text::CSV_XS; my $csv=Text::CSV_XS->new(); open my $fh, "<", "file.csv" or die "file.csv: $!"; my $sum = 0; while (my $row = $csv->getline ($fh)) {$sum += $row->[4]}; close $fh; print "$sum\n";}'

Here's the actual Perl code, for better readability

use Text::CSV_XS; # use the parser library
my $csv = Text::CSV_XS->new(); # Create parser object
open my $fh, "<", "file.csv" or die "file.csv: $!"; # Open the file. 
my $sum = 0; 
while (my $row = $csv->getline ($fh)) { # $row is array of field values now
    $sum += $row->[4];
}
close $fh; 
print "$sum\n";


The above could be shortened by using slightly lesser quality but denser Perl:

cat file.csv | perl -MText::CSV_XS -nae '$csv=Text::CSV_XS->new(); 
               $csv->parse($_); @f=$csv->fields(); $s+=$f[4]} { print "$s\n"'

这篇关于Perl脚本(或其它),至总量达CSV列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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