我有一個文本文件,在行首以空格作爲分隔符。僅從文本文件中的一行解析日期
沒有初始空格的行應放在CSV文件的第一列;有兩個空格的應該放在CSV的第二列;有四個空格的應該放在第三欄。
這是所有工作正常所需。
在以兩個空格開頭的行中,我希望只有日期應該放在第二列中,並放棄該行的其他數據。其餘的應該保持原樣。
爲了清楚起見,我在行的開頭標出了空格#
。
文本文件:
Component1
##(111) Amar Sen <[email protected]> <No comment> 2013/04/01
####/Com/src/folder1/folder2/newfile.txt
##(1199) Prashant Singh <[email protected]> <No comment> 2013/04/24
####/Com/src/folder1/folder2/testfile24
####/Com/src/folder1/folder2/testfile25
####/Com/src/folder1/folder2/testfile26
##(1204) Anthony Li <[email protected]> <No comment> 2013/04/25
####/Com/src2
Component2(added)
Component3
輸出格式:
Component1,2013/04/01,/Com/src/folder1/folder2/newfile.txt
2013/04/24,/Com/src/folder1/folder2/testfile24
/Com/src/folder1/folder2/testfile25
/Com/src/folder1/folder2/testfile26
2013/04/25,/Com/src2
Component2(added)
Component3
下面的代碼。它的工作很好,除了上面描述的變化。
use strict;
use warnings;
my $previous_count = "-1"; #beginning, we will think, that no spaces.
my $current_count = "0"; #current default value
my $maximum_count = 3;
my $to_written = "";
my $delimiter_between_columns = ",";
my $newline_separator = ";";
my $file = 'C:\\textfile.txt';
open (my $fh, '<:encoding(UTF-8)', $file) or die "Could not open file '$file' $!";
while (my $row = <$fh>) {
# ok, read.
chomp($row);
# print "row is : $row\n";
if ($row =~ m/^(\s*)/) {
#print length($1);
$current_count = length($1)/2; #take number of spaces divided by 2
$row =~ s/^\s+//;
if ($previous_count >= $current_count || $previous_count == $maximum_count) {
#output here
print "$to_written" . $newline_separator . "\n";
$previous_count = 0;
$to_written = "";
}
$previous_count = 0 if ($previous_count == -1);
$to_written .= $delimiter_between_columns x ($current_count - $previous_count) . "$row";
$previous_count = $current_count;
#print"\n";
}
}
print "$to_written" . $newline_separator . "\n";
「輸出格式」您發佈不符合你描述你想要的,在那裏你have'。由於CSV字段由逗號分隔,因此任何不含逗號的行表示所有內容都位於第一列。 – doubleDown