我有一个Perl程序,它根据正则表达式匹配捕获输入文件的第一行和第二行。
在我的输入文件中,我需要的细节中有标题符,并且它没有捕获我想要的行,所以第二行细节不存在。
是否可以更改我的程序,以便能够根据空格或第二行的位置捕获正确的细节线?
如果发生,则标题分割和第二行始终位于整个文件的相同位置。
下面是我到目前为止的脚本,但正如我所提到的那样,当行在详细信息中有一个标题分割时,它不会捕获第二行。
use strict;
my $find = '^(J0|JT)';
my $match = 0;
open( NEW, ">", "output.txt" ) or die "could not open:$!";
open( FILE, "<", "combined.txt" ) or die "could not open:$!";
while (<FILE>) {
chomp;
if ($match) {
$match = 0;
print NEW "$_\n";
}
if (/$find/) {
$match = 1;
print NEW "$_";
}
}
close(FILE);
close(NEW);
---------------------------------------------------------------------------------
JT00000000 DOE, JANE 01/23/16 J00000000 1 01/27/16
U# NUMBER UNIT # FAIL
---------------------------------------------------------------------------------
JT00000000 DOE, JOHN 01/24/16 J00000000 1 01/28/16
CLAIM DETAIL
PATIENT ADM/SER DIS DATE UNIT # NUM BILL DATE
CHECKS DESCRIPTION VALUE (FIRST)
TOB AFTER UNIT # FAIL
----------------------------------------------------------------------------------
J000000000 DOE, JOHN 01/25/16 01/31/16 J00000000 1 01/29/16
TXN MISS UNIT # FAIL
JT00000000 DOE, JANE 01/23/16 J00000000 1 01/27/16 U# NUMBER UNIT # FAIL
JT00000000 DOE, JOHN 01/24/16 J00000000 1 01/28/16 TOB AFTER FAIL
J000000000 DOE, JOHN 01/25/16 01/31/16 J00000000 1 01/29/16 TXN MISS FAIL
答案 0 :(得分:0)
这仅打印标题符之间的第一行和最后一行。它会保留所有行的缓冲区,直到下一次休息。请注意,我在最后添加了一个标题中断。
use warnings;
use strict;
my @lines;
while (<DATA>) {
chomp;
if (/^-/) {
if (@lines) {
print @lines[0, -1], "\n";
}
@lines = ();
}
else {
push @lines, $_;
}
}
__DATA__
---------------------------------------------------------------------------------
JT00000000 DOE, JANE 01/23/16 J00000000 1 01/27/16
U# NUMBER UNIT # FAIL
---------------------------------------------------------------------------------
JT00000000 DOE, JOHN 01/24/16 J00000000 1 01/28/16
CLAIM DETAIL
PATIENT ADM/SER DIS DATE UNIT # NUM BILL DATE
CHECKS DESCRIPTION VALUE (FIRST)
TOB AFTER UNIT # FAIL
----------------------------------------------------------------------------------
J000000000 DOE, JOHN 01/25/16 01/31/16 J00000000 1 01/29/16
TXN MISS UNIT # FAIL
----------------------------------------------------------------------------------
答案 1 :(得分:0)
我会写这个。它只是跳过不以J0
或JT
开头的每一行,否则它会打印当前行并读取并打印下一行
编写一个从STDIN读取并写入STDOUT的程序几乎总是更简单。这样你就可以通过运行
来测试它$ perl report.pl combined.txt
将输出打印到您的控制台,或
$ perl report.pl combined.txt > output.txt
如果您想将结果写入原始程序的output.txt
use strict;
use warnings 'all';
while ( <> ) {
next unless /^J[0T]/;
print;
print scalar <>;
print "\n";
}
JT00000000 DOE, JANE 01/23/16 J00000000 1 01/27/16
U# NUMBER UNIT # FAIL
JT00000000 DOE, JOHN 01/24/16 J00000000 1 01/28/16
J000000000 DOE, JOHN 01/25/16 01/31/16 J00000000 1 01/29/16
TXN MISS UNIT # FAIL