2010-03-06 69 views

回答

3

假设INPUT_RECORD_SEPARATOR还没有被重新定义,换行符大概可以通过$取代。然后,你应该能够chomp输入行的副本去除换行符。 编辑:然后预先输出\n

while (<FPTR>) # While still input lines in the file... 
{ 
    if (/\x1b&[email protected]$/) { 
    $aa = $_; 
    chomp $aa; 
    print STDOUT "\n" . $aa; 
    } 
... 
} 

这将消除您的正则表达式的多个副本简化代码。

+1

问题是您的版本不会将换行符移动到像原始脚本那样的模式之前。 – rsp 2010-03-06 21:05:39

+0

@rsp:我同意。我完全错过了///'分隔符右边的换行符。我已经更新了我的答案。 – toolic 2010-03-06 23:21:11

0

该代码似乎寻找一个字节序列,然后换行,并在模式前面交换newkline。您可以使用\n为换行符(PR \r\n如果脚本是在DOS格式),follwoing相当于我认为:

while (<FPTR>) # While still input lines in the file... 
{ 
    if($_ =~ m/\x1b&[email protected]\n/) 
    { 
    $aa=$_; 
    $aa =~ s/\x1b&[email protected]\n/\n\x1b&[email protected]/; 
    print STDOUT $aa; 
    } 
... 
} 
0

尝试做一格格()首先处理之前

while (<FPTR>) { 
    chomp; 
    .... 
} 
+1

但是,只有当该行以'ESC&d @'结尾时才需要chomp。 – 2010-03-06 14:18:19

0

这是一个棘手:

/\x1b&[email protected] 
/

这就是转义字符后“& d @”在该行的末尾。因此,我可以实际使用:

while (my $line = <FPTR>) { 
    if ($line =~ /x1b&[email protected]$/) { 
     chomp $line; 
     print $line; 
    } 
} 

我不明白为什么有必要做的$_副本中$aa,但如果这真的是必要的,使用

if ((my $copy = $line) =~ /x1b&[email protected]$/) { 
     chomp $copy; 
     print $copy; 
    } 
} 

你只需要指定STDOUT作为文件句柄,如果某个其他文件句柄已被选为默认文件句柄。