-2

假设我有一个文件夹,其中包含一千个名为 File1.csv、File2.csv、...、File1000.csv 的文件,每个文件都包含几行以分号分隔 (;) 的数据值。

我需要一个 Perl 脚本来将该文件夹中的所有 csv 文件“合并”为一个,方法是一个接一个地附加每个文件,并在每一行的末尾添加另一个数据列,其中包含当前正在处理的文件的名称(没有结尾,例如“;File2”)。

史蒂夫

4

1 回答 1

1

Text::CSV可用于解析 CSV。以下脚本将从包含 CSV 文件的目录中运行。它不是递归的(aglob已被使用)。如果你需要它递归查找文件,你可以使用File::FindPerl 模块。

#!/usr/bin/env perl

use strict;
use warnings;

use Text::CSV;

my $csv = Text::CSV->new( { 'sep_char' => ';' } );

open my $fho, '>', 'combined.csv' or die "Error opening file: $!";

while ( my $file = <*.csv> ) {
    open my $fhi, '<', $file or die "Error opening file: $!";
    ( my $last_field = $file ) =~ s/\.[^\.]+$//;  # Strip the file extension off

    while ( my $row = $csv->getline($fhi) ) {
        $csv->combine( @$row, $last_field );  # Construct new row by appending the file name without the extension
        print $fho $csv->string, "\n";        # Write the combined string to combined.csv
    }
}
于 2012-05-28T11:22:02.650 回答