在perl中使用csv模块删除匹配行

在perl中使用csv模块删除匹配行,perl,Perl,到目前为止,我已经知道如何使用这个perl模块来匹配csv文件中的一行,Text::csvxs。 我需要的帮助是删除文件中的那一行。有没有一个简单的方法可以做到这一点?在本模块中是否有实现此目的的方法 use strict; use warnings; use Text::CSV_XS; my @rows; my $csv = Text::CSV_XS->new ({ binary => 1 }) or die "Cannot use CSV: ".Text::CSV_X

到目前为止,我已经知道如何使用这个perl模块来匹配csv文件中的一行,
Text::csvxs
。 我需要的帮助是删除文件中的那一行。有没有一个简单的方法可以做到这一点?在本模块中是否有实现此目的的方法

use strict;
use warnings;
use Text::CSV_XS;

my @rows;
my $csv = Text::CSV_XS->new ({ binary => 1 }) or
     die "Cannot use CSV: ".Text::CSV_XS->error_diag ();
open my $fh, "<:encoding(UTF-16LE)", "Test.txt" or die "cannot open file: $!";
while (my $row = $csv->getline ($fh)) {

    if ($row->[0] =~ m/ABCDE/)
    {
      print "We have a match, remove the line \n";
    }
    else
    {
        print "No match found\n";
    }
}
$csv->eof or $csv->error_diag ();
close $fh;
使用严格;
使用警告;
使用Text::csvxs;
我的@行;
我的$csv=Text::csv_XS->new({binary=>1})或
die“无法使用CSV:”.Text::CSV_XS->错误诊断();

打开我的$fh,“看起来该模块不能做到这一点,但还有许多其他Perl模块可以做到:


有关文档和示例,请参见链接。

该模块似乎不能做到这一点,但还有许多其他Perl模块可以做到:


有关文档和示例,请参阅链接。

我不确定Text::CSV_XS是否允许您删除行。我认为它不允许。
删除行的一种方法是在while循环中读取行时,只存储所需的行。
关闭文件后,只需使用要保留在文件中的所有行覆盖即可。
因此,在您的情况下,任何与ABCDE不匹配的内容都应保存,稍后再写回同一文件。

因此,您最终将阅读10行,然后写回9行(假设其中一行有ABCDE)。

我不确定Text::CSV_XS是否允许您删除行。我认为它不允许。
删除行的一种方法是在while循环中读取行时,只存储所需的行。
关闭文件后,只需使用要保留在文件中的所有行覆盖即可。
因此,在您的情况下,任何与ABCDE不匹配的内容都应保存,稍后再写回同一文件。

因此,你最终会读10行,写9行(假设其中一行有ABCDE).

从文件中删除内容的唯一方法是读取文件并写出新内容。通常的方法是打开另一个文件进行写入,从输入文件中读取记录,然后只将您希望保留的记录写入输出文件。然后是两者,或原始记录(取决于您是否要保留备份),然后将输出文件转换为原始输入文件名。

从文件中删除内容的唯一方法是读取文件并写出新内容。执行此操作的常用方法是打开另一个文件进行写入,读取输入文件中的记录,并仅将您希望保留的记录写入输出文件。然后是两者或源文件al(取决于您是否要保留备份),然后将输出文件转换为原始输入文件名。

首先,我将解析CSV文件

use Text::CSV_XS qw( csv );
$parsed_file_array_of_hashesv = csv(
    in      => "$input_csv_filename",
    sep     => ';',
    headers => "auto"
);    # as array of hash
第二,一旦拥有了hashesv的$parsed_file_array_,现在就可以在perl中循环该数组,并检测要从数组中删除的行。 然后使用

拼接阵列、偏移、长度

通过索引偏移+长度从偏移索引中删除任何内容

让我们假设索引为0

my @extracted_array = @$parsed_file_array_of_hashesv;    #dereference hashes reference
splice @extracted_array, 0, 1;#remove entry 0
$ref_removed_line_parsed = \@extracted_array;            #referece to array
第三,将数组写回CSV文件

$current_metric_file    = csv(
    in      => $ref_removed_line_parsed,                 #only accepts referece
    out     => "$output_csv_filename",
    sep     => ';',
    eol     => "\n",                                   # \r, \n, or \r\n or undef
    #headers => \@sorted_column_names,              #only accepts referece
    headers => "auto"
); 
请注意,如果使用\@sorted_column_名称,则可以控制列的顺序

my @sorted_column_names;
foreach my $name (sort {lc $a cmp lc $b} keys %{ $parsed_file_array_of_hashesv->[0] }) { #all hashes have the same column names so we choose the first one
    push(@sorted_column_names,$name);
}

那应该不用你的代码就可以写CSV文件了。

首先,我要解析CSV文件

use Text::CSV_XS qw( csv );
$parsed_file_array_of_hashesv = csv(
    in      => "$input_csv_filename",
    sep     => ';',
    headers => "auto"
);    # as array of hash
第二,一旦拥有了hashesv的$parsed_file_array_,现在就可以在perl中循环该数组,并检测要从数组中删除的行。 然后使用

拼接阵列、偏移、长度

通过索引偏移+长度从偏移索引中删除任何内容

让我们假设索引为0

my @extracted_array = @$parsed_file_array_of_hashesv;    #dereference hashes reference
splice @extracted_array, 0, 1;#remove entry 0
$ref_removed_line_parsed = \@extracted_array;            #referece to array
第三,将数组写回CSV文件

$current_metric_file    = csv(
    in      => $ref_removed_line_parsed,                 #only accepts referece
    out     => "$output_csv_filename",
    sep     => ';',
    eol     => "\n",                                   # \r, \n, or \r\n or undef
    #headers => \@sorted_column_names,              #only accepts referece
    headers => "auto"
); 
请注意,如果使用\@sorted_column_名称,则可以控制列的顺序

my @sorted_column_names;
foreach my $name (sort {lc $a cmp lc $b} keys %{ $parsed_file_array_of_hashesv->[0] }) { #all hashes have the same column names so we choose the first one
    push(@sorted_column_names,$name);
}

那应该写CSV文件而不写你的行。

+1这也是我的建议。还有,似乎是最近更新的。Tie::CSV_文件是2003年的,可能已经过期了。+1这也是我的建议。还有,似乎是最近更新的。Tie::CSV_文件是2003年的,可能已经过期了.