使用Perl将列转换为行

时间:2014-12-24 09:08:55

标签: perl

我有下面的文件,想要从特定列的列到列转置它。 并在列中填充转置列,例如Amount。

输入文件(第一行是列名)

Market,Merchant,SE_mismatch,Dupe,SENumber,Local,Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec

Austria,Asfinag,No,1,9406589846,EUR,1125,1125,1125,1125,1125,1125,1125,1125,1125,1125,1125,1125
Austria,BÖGTY,Yes,1,9406589844,EUR,844,844,844,844,844,844,844,844,844,844,844,844
Austria,Gault,No,1,9496732942,EUR,3375,3375,3375,3375,3375,3375,3375,3375,3375,3375,3375,3375

输出(第一行是列名)

Market, Merchant,SE_mismatch,Dupe,SENumber,Local,Amount

Austria, Asfin, No, 1,9406589846,EUR, 1125,1
Austria, Asfin, No, 1,9406589846,EUR, 1125,2 
Austria, Asfin, No, 1,9406589846,EUR, 1125,3 
Austria, Asfin, No, 1,9406589846,EUR, 1125,4
Austria, Asfin, No, 1,9406589846,EUR, 1125,5 
Austria, Asfin, No, 1,9406589846,EUR, 1125,6 
Austria, Asfin, No, 1,9406589846,EUR, 1125,7 
Austria, Asfin, No, 1,9406589846,EUR, 1125,8 
Austria, Asfin, No, 1,9406589846,EUR, 1125,9
Austria, Asfin, No, 1,9406589846,EUR, 1125,10 
Austria, Asfin, No, 1,9406589846,EUR, 1125,11  
Austria, Asfin, No, 1,9406589846,EUR, 1125,12 
Austria, BÖWG, Yes, 1,9406589844,EUR, 844,1 
Austria, BÖWG, Yes, 1,9406589844,EUR, 844,2 
Austria, BÖWG, Yes, 1,9406589844,EUR, 844,3 
Austria, BÖWG, Yes, 1,9406589844,EUR, 844,4 
Austria, BÖWG, Yes, 1,9406589844,EUR, 844,5  
Austria, BÖWG, Yes, 1,9406589844,EUR, 844,6 
Austria, BÖWG, Yes, 1,9406589844,EUR, 844,7  
Austria, BÖWG, Yes, 1,9406589844,EUR, 844,8  
Austria, BÖWG, Yes, 1,9406589844,EUR, 844,9 
Austria, BÖWG, Yes, 1,9406589844,EUR, 844,10  
Austria, BÖWG, Yes, 1,9406589844,EUR, 844,11 
Austria, BÖWG, Yes, 1,9406589844,EUR, 844,12 
Austria, Gault, No, 1,9496732942,EUR, 3375,1
Austria, Gault, No, 1,9496732942,EUR, 3375,2 
Austria, Gault, No, 1,9496732942,EUR, 3375,3
Austria, Gault, No, 1,9496732942,EUR, 3375,4
Austria, Gault, No, 1,9496732942,EUR, 3375,5
Austria, Gault, No, 1,9496732942,EUR, 3375,6
Austria, Gault, No, 1,9496732942,EUR, 3375,7 
Austria, Gault, No, 1,9496732942,EUR, 3375,8 
Austria, Gault, No, 1,9496732942,EUR, 3375,9 
Austria, Gault, No, 1,9496732942,EUR, 3375,10
Austria, Gault, No, 1,9496732942,EUR, 3375,11
Austria, Gault, No, 1,9496732942,EUR, 3375,12

我已通过在每行末尾添加新列来更新上述输出。 我在代码下面尝试了modifiyng。

#!/usr/bin/perl
#This is to Transpose the Data from Column to row.

use strict;
use warnings;

my $fn = shift || "testf1";
my $specific_column_number = shift || 1;
my $count = 1;
my $n = 0;

open( my $fh, "<", $fn) or die "Could not open file '$fn' : $!\n";
my @head = split (/,/, scalar <$fh>);
print join(",", @head[0..($specific_column_number - 1)]) . "\n";
while (<$fh>) {
    my @fields = split /,/;
    next if @fields == 0;
    my $start_of_row = 
       join(",",(@fields[0..($specific_column_number -1)]));
    my @rest_of_fields = @fields[$specific_column_number..$#fields];
    for (@rest_of_fields) {
        print "$start_of_row,$_,$count\n";
        $n++;
        $count++;
    }
}
close ($fh);

但是它按照下面的增量顺序来划线。

Market, Merchant,SE_mismatch,Dupe,SENumber,Local
Austria, Asfin, No, 1,9406589846,EUR, 1125,1
Austria, Asfin, No, 1,9406589846,EUR, 1125,2 
Austria, Asfin, No, 1,9406589846,EUR, 1125,3 
Austria, Asfin, No, 1,9406589846,EUR, 1125,4
Austria, Asfin, No, 1,9406589846,EUR, 1125,5 
Austria, Asfin, No, 1,9406589846,EUR, 1125,6 
Austria, Asfin, No, 1,9406589846,EUR, 1125,7 
Austria, Asfin, No, 1,9406589846,EUR, 1125,8 
Austria, Asfin, No, 1,9406589846,EUR, 1125,9
Austria, Asfin, No, 1,9406589846,EUR, 1125,10 
Austria, Asfin, No, 1,9406589846,EUR, 1125,11  
Austria, Asfin, No, 1,9406589846,EUR, 1125,12 
Austria, BÖWG, Yes, 1,9406589844,EUR, 844,13
Austria, BÖWG, Yes, 1,9406589844,EUR, 844,14
Austria, BÖWG, Yes, 1,9406589844,EUR, 844,15
Austria, BÖWG, Yes, 1,9406589844,EUR, 844,16
Austria, BÖWG, Yes, 1,9406589844,EUR, 844,17
........ and so on 

请指导我或提出任何建议。

由于

1 个答案:

答案 0 :(得分:2)

以下是解决方案的模板:

use strict;
use warnings;

my $fn = 'File1';
my $specific_column_number = 6;
my $new_column_name = 'Amount';

open( my $fh, "<", $fn) or die "Could not open file '$fn' : $!\n";
my @head = split (/,/, scalar <$fh>);
print join(",", @head[0..($specific_column_number - 1)]) . "\n";
while (<$fh>) {
    my @fields = split /,/;
    next if @fields == 0;
    my $start_of_row = 
       join(",", (@fields[0..($specific_column_number -1)], $new_column_name));
    my @rest_of_fields = @fields[$specific_column_number..$#fields];
    for (@rest_of_fields) {
        print "$start_of_row, $_\n";
    }
}
close ($fh);