3

我有一个包含大约 70,000 条记录的文件,其结构大致如下:

01499     1000642   4520101000900000
...more numbers...
104000900169
+Fieldname1
-Content
+Fieldname2
-Content
-Content
-Content
+Fieldname3
-Content
-Content
+Fieldname4
-Content
+Fieldname5
-Content
-Content
-Content
-Content
-Content
-Content

01473     1000642   4520101000900000
...more numbers...

编辑 1:因此,每条记录都以一列数字开头,并以空行结尾。在此空白行之前,大多数记录都有+Fieldname5一行或多-Content行。

我想做的是将所有多行条目合并到一行中,同时用空格替换前导减号字符,除了那些与最后一个字段有关的字符(即本例中的 Fieldname5)。

它应该是这样的:

01499     1000642   4520101000900000
...more numbers...
104000900169
+Fieldname1
-Content
+Fieldname2
-Content Content Content
+Fieldname3
-Content Content
+Fieldname4
-Content
+Fieldname5
-Content
-Content
-Content
-Content
-Content
-Content

01473     1000642   4520101000900000
...more numbers...

我现在拥有的是这个(改编自这个答案):

use strict;
use warnings;

our $input = "export.txt";
our $output = "export2.txt";

open our $in, "<$input" or die "$!\n"; 
open our $out, ">$output" or die "$!\n"; 

my $this_line = "";
my $new = "";

while(<$in>) {
    my $last_line = $this_line;
    $this_line = $_;

    # if both $last_line and $this_line start with a "-" do the following:
    if ($last_line =~ /^-.+/ && $this_line =~ /^-.+/) {

        #remove \n from $last_line
        chomp $last_line;

        #remove leading "-" from $this_line
        $this_line =~ s/^-//;

        #join both lines and print them to the file
        $new = join(' ', $last_line,$this_line);
        print $out $new;
        } else {
        print $out $last_line;
            }
    }
close ($in);
close ($out);

但这有两个问题:

  • 它正确打印出连接线,但仍然打印出第二行,例如

    +Fieldname2 -Content 内容内容 -Content

那么如何让脚本只输出连接线呢?

  • 它一次只能处理两行,而一些多行条目最多有 40 行。

编辑 2:因此,我的问题是如何执行以下操作:

  1. 逐行读取文件并将其写入输出文件
  2. 当出现多行部分时,一口气读取并处理它,替换\n-,除非它属于给定的字段名(例如Fieldname5)。
  3. 再次回到读写每一行,直到出现另一个多行块

编辑 3: 它奏效了!我只是在开头添加了另一个条件:use strict; 使用警告;

our $input = "export.txt";
our $output = "export2.txt";

open our $in, "<$input" or die "Kann '$input' nicht finden: $!\n"; 
open our $out, ">$output" or die "Kann '$output' nicht erstellen: $!\n"; 


my $insideMultiline = 0;
my $multilineBuffer = "";
my $exception = 0;                  # variable indicating whether the current multiline-block is a "special" or not

LINE:
while (<$in>) {
    if (/^\+Fieldname5/) {          # if line starts with +Fieldname5, set $exception to "1"
        $exception = 1;
    } 
    elsif (/^\s/) {                 # if line starts with a space,  set $exception to "0"
        $exception = "0";
    }
    if ($exception == 0 && /^-/) {  # if $exception is "0" AND the line starts with "-", do the following
        chomp;
        if ($insideMultiline) {
            s/^-/ /;
            $multilineBuffer .= $_;
        }
        else {
            $insideMultiline = 1;
            $multilineBuffer = $_;
        }
        next LINE;
    }
    else {
        if ($insideMultiline) {
            print $out "$multilineBuffer\n";
            $insideMultiline = 0;
            $multilineBuffer = "";
        }
        print $out $_;
        }
}

close ($in);
close ($out);
4

1 回答 1

2

假设唯一以“-”开头的行是这些多行部分,你可以这样做......

# Open $in and $out as in your original code...

my $insideMultiline = 0;
my $multilineBuffer = "";

LINE:
while (<$in>) {
    if (/^-/) {
        chomp;
        if ($insideMultiline) {
            s/^-/ /;
            $multilineBuffer .= $_;
        }
        else {
            $insideMultiline = 1;
            $multilineBuffer = $_;
        }
        next LINE;
    }
    else {
        if ($insideMultiline) {
            print $out "$multilineBuffer\n";
            $insideMultiline = 0;
            $multilineBuffer = "";
        }
        print $out $_;
    }
}

至于嵌入的子问题(“除了那些与最后一个字段有关的问题”),我需要更多关于文件格式的详细信息才能做到这一点。看起来像一个空白行将一组字段和内容彼此分开,但在描述中并不是 100% 清楚。不过,上面的代码应该处理您在底部列出的要求。

于 2011-07-20T00:14:53.630 回答