【发布时间】:2015-10-14 17:18:35
【问题描述】:
根据您的看法,我需要根据 Id 是否唯一来删除行或如果 Id 有重复项则提取行(保留所有重复项)。 而且我不确定/没有足够的 Perl 知识来完成这个。我找到了类似的主题,但没有太多成功。这些是我使用example 1、example 2 和example 3 的示例。在上一个问题中,有人向我展示了 List::MoreUtils 模块的解决方案,因此我可以将值与通用 ID 合并。现在不是这种情况,如果 id 是唯一的,这将删除行。我知道我可能可以使用 List::MoreUtils 模块来做到这一点,但我想不这样做。这是我的虚拟数据(从其他问题复制的示例数据,因为数据无关紧要),在这里你可以看到我所追求的。顺序并不重要。
之前:
Cat_id;Cat_name;Id;Name;Amount;Colour;Bla
101;Fruits;50010;Grape;500;Red;1
101;Fruits;50020;Strawberry;500;Red;1
201;Vegetables;60010;Carrot;500;White;1
101;Fruits;50060;Apple;1000;Red;1
101;Fruits;50030;Banana;1000;Green;1
101;Fruits;50060;Apple;500;Green;1
101;Fruits;50020;Strawberry;1000;Red;1
201;Vegetables;60010;Carrot;100;Purple;1
101;Fruits;50020;Strawberry;200;Red;1
之后:
Cat_id;Cat_name;Id;Name;Amount;Colour;Bla
101;Fruits;50020;Strawberry;500;Red;1
201;Vegetables;60010;Carrot;500;White;1
101;Fruits;50060;Apple;1000;Red;1
101;Fruits;50060;Apple;500;Green;1
101;Fruits;50020;Strawberry;1000;Red;1
201;Vegetables;60010;Carrot;100;Purple;1
101;Fruits;50020;Strawberry;200;Red;1
您可以看到 ID 为 50010 和 50030 的 Grape 和 Banana 行已被删除,因为两者只存在一个条目。
这是我的脚本,我正在努力从哈希中选择唯一值并输出它们的部分(考虑到 Text::CSV_XS 模块)。有人可以告诉我怎么做吗?
#!/usr/bin/perl -w
use strict;
use warnings;
use Text::CSV_XS;
my $inputfile = shift || die "Give input and output names!\n";
my $outputfile = shift || die "Give output name!\n";
open (my $infile, '<:encoding(iso-8859-1)', $inputfile) or die "Sourcefile in use / not found :$!\n";
open (my $outfile, '>:encoding(UTF-8)', $outputfile) or die "Outputfile in use :$!\n";
my $csv_in = Text::CSV_XS->new({binary => 1,sep_char => ";",auto_diag => 1,always_quote => 1,eol => $/});
my $csv_out = Text::CSV_XS->new({binary => 1,sep_char => "|",auto_diag => 1,always_quote => 1,eol => $/});
my $header = $csv_in->getline($infile);
$csv_out->print($outfile, $header);
my %data;
while (my $elements = $csv_in->getline($infile)){
my @columns = @{ $elements };
my $id = $columns[2];
push @{ $data{$id} }, \@columns;
}
for my $id ( sort keys %data ){ # Sort not important
if @{ $data{$id} } > 1 # Here I have no idea anymore..
$csv_out->print($outfile, \@columns); #
}
【问题讨论】:
-
这个问题看起来有点眼熟。 stackoverflow.com/questions/28627669/…
-
@Sobrique 同意,几乎一样.. 我试图解决那个问题,但是如果 id 相同,那就是合并字段,如果 id 是唯一的,这个是删除行