大文件删除Perl中的多行

时间:2013-08-08 19:49:58

标签: perl parsing file-io while-loop

我有一个大文件,我想删除使用Perl的多行,所以不能选择slurping。

它的spice文件是这样的:

X1 param param param
+param param param
+param param param
X2 param param param
+param param param
+param param param
+param param param
X3 param param param
+**target_param** param param param
+param param param
+param param param
X4 param **target_param** param param
+param param param
+param param param

目标可以是任何地方,并且任何实例,即以X(N)开头但不包括X(N + 1)的任何行都需要从输出文件中省略。

示例输出:

X1 param param param
+param param param
+param param param
X2 param param param
+param param param
+param param param
+param param param

1 个答案:

答案 0 :(得分:1)

这可能是一个解决方案吗?它不包括target_parameter的第二次出现。

更新:解决方案现在只在'块'中查找target_parameter,而不是在连续块中查找(因为我第一次理解这个问题)。

#!/usr/bin/perl
use strict;
use warnings;

{
    local $/ = 'X';
    <DATA>; # discard first block (empty)
    while (<DATA>) {
        chomp;
        print "X$_" unless /target_param/;
    }   
}
__DATA__
X1 param param param
+param param param
+param param param
X2 param param param
+param param param
+param param param
+param param param
X3 param param param
+**target_param** param param param
+param param param
+param param param
X4 param **target_param** param param
+param param param
+param param param
X5 param param
+param param param
X6 param param param
+param param param **target_param**
+param param param
+param param param
X7 param **target_param** param param
+param param param
+param param param
X8 param param param
+param param param
+param param param

此数据的输出为:

C:\Old_Data\perlp>perl try.pl
X1 param param param
+param param param
+param param param
X2 param param param
+param param param
+param param param
+param param param
X5 param param
+param param param
X8 param param param
+param param param
+param param param