我有这些csv文件。想通过从每个csv中提取特定水果的某些字段来创建一个csv。 例如,对于苹果,从file2中选择第3个字段,从file3中选择第5个字段。 同样,对于其他水果,请选择相应的数据(file2的第三个字段,file3的第5个字段)。
file1
apple,60
橙色40
葡萄,80
香蕉,70
荔枝100
file2
orange,ab,2
葡萄,ab,4
banana,b,6
apple,a,8
litchi,d,10
文件3 香蕉,ab,c,d,ef
apple,ab,c,d,ef
荔枝,d,e,f,g
橙色,d,e,f,g
葡萄,d,e,f,g
输出:
apple,8,d,ef
orange,2,f,g
葡萄,4,f,g
banana,6,d,ef
荔枝10,f,g
代码:
my $File1 = '';
my $File2 = '';
my $File3 = '';
GetOptions ('file=s' => \$File1, 'file=s' => \$file2, 'file=s' => \$File3);
return ($File1,$File2,$File3);
}
sub append {
my ($File1,$File2,$File3) = @_;
my ($File) = @_ ;
open (FH,"<$File") or die ;
while (<FH>) {
chomp ;
my $line = $_ ;
my @arr = split /,/,$line ;
my $temp = "" ;
my $param = $arr[0] ;
$temp = "$arr[0]\n" ;
}
open (FH,"<$File2") or die ;
while (<FH>) {
chomp ;
my $line = $_ ;
my @columns = split/,/, $line ;
my $temp1 = "" ;
my $fruits = "" ;
foreach my $line(@arr){
if ($columns[0] =~ /fruits/) {
my $var2 = $arr[1] ;
$temp1 = "$arr[2]\n" ;
}
open (FH,"<$File3") or die ;
while (<FH>) {
chomp ;
my $line = $_ ;
my @arr = split /,/,$line ;
my $temp2 = "" ;
foreach my $line(@arr){
my $param = "" ;
my $temp = "" ;
my $temp1 = "" ;
if (defined $hashparam{@columns}){
my $var2 = $arr[5] ;
$temp2 = "$arr[5]\n" ;
print FH "$temp,$temp1,$temp2\n";
}
}