我有一个代码,允许我处理一个文件,在该文件中,他搜索所有以" false"开头的行。并将它们归还给我 我希望每一行都有" false"存储在文件中,即。
#!/usr/bin/perl
use warnings;
use strict;
open(DATA, '<', 'DATA.csv');
my @a = <DATA>;
my @b = grep(/false/, @a);
chomp(@b);
for (my $i=0; $i <= $#b; $i++) {
print $i + 1;
print ". ";
print split('false',"$b[$i]\n");
}
__________________ DATA ____________________________________
2015.07.23 16:47:23
false, false, true, false, true, false,
defau, ame_a, ame_m, ame_uc, ame_f, ams_l, en_affi,
true DPA01, fault, fault, fault, fault, fault, 1,
false DPA02, dfgeh, , , , fault, 1,
true DPA03, fault, fault, olt, fault, fault, 1,
false DPA01, fault, at, fault, 2, fault, 1,
false DPA02, dfgeh, , , , fault, 1,
true DPA03, fault, fault, olt, fault, fault, 1,
___ DPA02 ___________________
dfgeh
fault
1
___ DPA01 ________________
fault
at
fault
2
fault
1
___ DPA02 _____________
dfgeh
fault
1
答案 0 :(得分:0)
我认为你要做的是:
你在那里的程序根本不会这样做。
我首先建议,如果要安全地处理CSV文件,请使用CPAN中的Text :: CSV模块。如果你这样做,这个过程会变得简单得多:
use strict;
use warnings;
use Text::CSV;
my $csv = Text::CSV->new;
open my $csv_file, "<", "DATA.csv" or die "Can't open CSV file: $!\n";
while (my $row = $csv->getline($csv_file)) {
# First item is significator plus filename.
my($significator_and_file) = clean($row->[0]);
my ($significator, $file) = split /\s+/, $significator_and_file;
next unless $significator and $significator eq 'false';
open my $per_line_fh, ">>", $file or die "Can't open per-line file $file: $!\n";
# One item per line, plus final newline.
shift @$row;
print $per_line_fh join "\n", map { clean($_) } @$row;
print $per_line_fh "\n";
close $per_line_fh;
}
sub clean {
my $string = shift;
$string =~ s/^\s+//;
$string =~ s/\s+$//;
return $string;
}