仅从文本文件中的一行解析日期

时间:2013-06-12 11:57:04

标签: perl

我有一个文本文件,在行的开头有分隔符作为空格。

没有初始空格的行应该放在CSV文件的第一列;具有两个空格的那些应该进入CSV的第二列;那些有四个空格的人应该进入第三栏。

根据需要,这一切都正常。

在以两个空格开头的行中,我希望只有日期应该放在第二列中,丢弃该行的其他数据。其余部分应保持原样。

为了清楚起见,我在行的开头用#表示空格。

文字档案:

Component1
##(111) Amar Sen <amar.sen@gmail.com> <No comment> 2013/04/01
####/Com/src/folder1/folder2/newfile.txt
##(1199) Prashant Singh <psinsgh@gmail.com> <No comment> 2013/04/24
####/Com/src/folder1/folder2/testfile24
####/Com/src/folder1/folder2/testfile25
####/Com/src/folder1/folder2/testfile26
##(1204) Anthony Li <anthon.li@gmail.com> <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";

1 个答案:

答案 0 :(得分:1)

你似乎已经把自己与你的解决方案捆绑在了一起。

这个程序似乎可以满足您的需求。我在您的“输出格式”中添加了一些逗号,因为您的示例没有初始空字段的占位符。

为此,我保留了哈希字符。显然,为空格更改它们是微不足道的,将s/^(#*)//替换为s/^(\s*)//

use strict;
use warnings;

my @row;

while (<DATA>) {

  chomp;
  s/^(#*)//;
  my $i = length($1) / 2;

  if ($i == 1 and m<(\d{4}/\d{2}/\d{2})>) {
    $row[$i] = $1;
  }
  else {
    $row[$i] = $_;
  }

  if ($i == 2) {
    print join(',', @row), ";\n";
    @row = ('') x 3;
  }
}


__DATA__
Component1
##(111) Amar Sen <amar.sen@gmail.com> <No comment> 2013/04/01
####/Com/src/folder1/folder2/newfile.txt
##(1199) Prashant Singh <psinsgh@gmail.com> <No comment> 2013/04/24
####/Com/src/folder1/folder2/testfile24
####/Com/src/folder1/folder2/testfile25
####/Com/src/folder1/folder2/testfile26
##(1204) Anthony Li <anthon.li@gmail.com> <No comment> 2013/04/25
####/Com/src2

<强>输出

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;

<强>更新

将第一列和第二列中的值级联到未提供它们的后续行更有意义。如果从我的程序中删除行@row = ('') x 3,它将使用此输出

Component1,2013/04/01,/Com/src/folder1/folder2/newfile.txt;
Component1,2013/04/24,/Com/src/folder1/folder2/testfile24;
Component1,2013/04/24,/Com/src/folder1/folder2/testfile25;
Component1,2013/04/24,/Com/src/folder1/folder2/testfile26;
Component1,2013/04/25,/Com/src2;