1

好吧..我又卡住了。我已经阅读了很多类似问题的主题,但没有找到适合我的解决方案。我有一个 ; 分隔的 csv 文件和第 8 列 ($elements[7]) 的字符串如下:“aaaa;bb;cccc;ddddd;eeee;fffff;gg;”。我正在尝试基于拆分字符串;并将输出捕获到变量。然后在他们自己的列中使用主 csv 文件中的这些变量。

所以现在文件是这样的:

3d;2f;7j;8k;4s;2b;5g;"aaaa;bb;cccc;ddddd;eeee;fffff;gg;";4g;1a;5g;2g;7h;3d;2f;7j 3c;9k; 5l;4g;1a;5g;3d;“aaaa;bb;cccc;dddd;eeee;fffff;gg;”;4g;1a;5g;2g;7h;3d;2f;7j 4g;1a;5g;2g; 7h;3d;8k;“aaaa;bb;cccc;dddd;eeee;fffff;gg;”;3d;2f;7j;8k;4s;2b;4g;1a

我希望它像:

3d;2f;7j;8k;4s;2b;5g;4g;1a;5g;2g;7h;3d;2f;7j;aaaa;bb;cccc;ddddd;eeee;fffff;gg 3c;9k;5l;4g ;1a;5g;3d;4g;1a;5g;2g;7h;3d;2f;7j;aaaa;bb;cccc;ddddd;eeee;fffff;gg; 4g;1a;5g;2g;7h;3d;8k;3d;2f;7j;8k;4s;2b;4g;1a;aaaa;bb;cccc;ddddd;eeee;fffff;gg;

这是我一直在尝试的代码。我知道..这太可怕了!但我希望有人可以帮助我?

use strict;
use warnings;

my $inputfile  = shift || die "Give files\n";
my $outputfile = shift || die "Give output\n";

open my $INFILE,  '<', $inputfile   or die "In use / Not found :$!\n";
open my $OUTFILE, '>', $outputfile  or die "In use :$!\n";

while (<$INFILE>) {
  s/"//g;
  my @elements = split /;/, $_;

    my ($varA, $varB, $varC, $varD, $varE, $varF, $varG, $varH) split (';', $elements[10]);
        $elements[16] = $varA;
        $elements[17] = $varB;
        $elements[18] = $varC;
        $elements[19] = $varD; 
        $elements[20] = $varE;
        $elements[21] = $varF;
        $elements[22] = $varG;
        $elements[23] = $varH;

my $output_line = join(";", @elements);
print $OUTFILE $output_line;
}

close $INFILE;
close $OUTFILE;

exit 0;

我也对我的陈述感到困惑,这应该不可能吧?我的意思是 $vars 在一个封闭的部分,所以应该不可能将它们写入 $elements?

编辑

这就是我根据 TLP 的建议调整代码的方式:

use strict;
use warnings;
use Text::CSV;

my $inputfile  = shift || die "Give files\n";
my $outputfile = shift || die "Give output\n";

open my $INFILE,  '<', $inputfile   or die "In use / Not found :$!\n";
open my $OUTFILE, '>', $outputfile  or die "In use :$!\n";

my $csv = Text::CSV->new({  # create a csv object
    sep_char => ";",    # delimiter
    eol => "\n",        # adds newline to print
});

while (my $row = $csv->getline($INFILE)) {      # $row is an array ref
my $line = splice(@$row, 10, 1);            # remove 8th line
$csv->parse($line);                         # parse the line
push @$row, $csv->fields();                 # push newly parsed fields onto       main array
$csv->print($OUTFILE, $row);
}

close $INFILE;
close $OUTFILE;

exit 0;
4

1 回答 1

0

您应该使用 CSV 模块,例如Text::CSV来解析您的数据。这是一个关于如何完成的简短示例。您可以用您自己的替换我在下面使用的文件句柄。

use strict;
use warnings;
use Text::CSV;

my $csv = Text::CSV->new({     # create a csv object
        sep_char => ";",   # delimiter
        eol => "\n",       # adds newline to print
    });

while (my $row = $csv->getline(*DATA)) {   # $row is an array ref
    my $line = splice(@$row, 7, 1);   # remove 8th line
    $csv->parse($line);               # parse the line
    push @$row, $csv->fields();       # push newly parsed fields onto main array
    $csv->print(*STDOUT, $row);
}

__DATA__
3d;2f;7j;8k;4s;2b;5g;"aaaa;bb;cccc;ddddd;eeee;fffff;gg;";4g;1a;5g;2g;7h;3d;2f;7j
3c;9k;5l;4g;1a;5g;3d;"aaaa;bb;cccc;ddddd;eeee;fffff;gg;";4g;1a;5g;2g;7h;3d;2f;7j
4g;1a;5g;2g;7h;3d;8k;"aaaa;bb;cccc;ddddd;eeee;fffff;gg;";3d;2f;7j;8k;4s;2b;4g;1a

输出:

3d;2f;7j;8k;4s;2b;5g;4g;1a;5g;2g;7h;3d;2f;7j;aaaa;bb;cccc;ddddd;eeee;fffff;gg;
3c;9k;5l;4g;1a;5g;3d;4g;1a;5g;2g;7h;3d;2f;7j;aaaa;bb;cccc;ddddd;eeee;fffff;gg;
4g;1a;5g;2g;7h;3d;8k;3d;2f;7j;8k;4s;2b;4g;1a;aaaa;bb;cccc;ddddd;eeee;fffff;gg;
于 2012-07-05T12:44:49.630 回答