【问题标题】:How to put remove certain line breaks in a file如何在文件中删除某些换行符
【发布时间】:2011-10-08 16:11:05
【问题描述】:

我有一个包含大约 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...

我现在拥有的是这个(改编自this answer):

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);

但这有两个问题:

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

    +字段名2 -内容内容 内容 -内容

那么我怎样才能让脚本只输出连接线呢?

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

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

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

编辑 3: 有效!我只是在开头添加了另一个条件: 使用严格; 使用警告;

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);

【问题讨论】:

    标签: perl replace line-breaks line-by-line


    【解决方案1】:

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

    # 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% 清楚。不过,上面的代码应该可以处理您在底部列出的要求。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-09-26
      • 2021-09-12
      • 2016-03-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多