-4

Example filenames:

example_12345_8943759847.csv
example_23456_9859877.csv
example_34567_92837458738.csv
example_12345_1165253.csv
example_23456_9983632652.csv
example_23456_2345.csv

I need to join files that have the same middle-number, eg. 23456.csv Thing is I don't know this middle-number so I suppose this has to be some variable? I imagine I would have to list filenames with the same middle-part-number and then output this list?

I have only Perl or sed to my disposal.

Thanks

4

1 回答 1

2

这是任务的一部分(在 Perl 中)。查找共享中间部分的文件组:

use strict;
use warnings;

my @files = qw/
    example_12345_8943759847.csv
    example_23456_9859877.csv
    example_34567_92837458738.csv
    example_12345_1165253.csv
    example_23456_9983632652.csv
    example_23456_2345.csv
/;

my %middles;

#This creates a hash. The keys are the middle number; 
#the values are arrays of filenames that share that middle number.
foreach (@files)
{
    push @{$middles{$1}},$_ if (/[a-z]+_(\d+)_\d+\.csv/);
}

#Now process the results
foreach my $middle (keys %middles)
{
    #Get a group of filenames sharing the same middle part.
    my @files_to_join = @{$middles{$middle}};

    #Join them here...
}

其余的取决于您所说的“加入”是什么意思。您可能会发现该Text::CSV模块有助于处理 CSV 文件。

于 2013-03-07T13:33:24.130 回答