我已经把头发拉了4天我现在仍然没有这个工作。我必须记录文件,我正在尝试从日志文件中提取范围数据并将每个范围保存到单独的文件中。如何从日志文件中提取范围数据并将每个范围保存到单独的文件中。
#! /usr/bin/perl/
use warnings;
use strict;
my $outdir = "/home/lord-ivan/Soures_Code/Perl/projects/EJ/Queue/";
my $somefile = "/home/lord-ivan/Soures_Code/Perl/projects/Data/EJDATA.LOG";
my $count = 0;
my $spew = 0;
my $out;
open (my $fh, "< $somefile" ) or die "Could not open file '$somefile' $!";
print "$somefile open\n";
while (<$fh>) {
if (/TRANSACTION STARTED/) {
open($out, '>', "${outdir}TRANSACTION$count.txt") or die $!;
$spew = 1;
$count++;
}
if ($spew) {
print $out $_;
}
if (/TRANSACTION END/) {
close($out);
$spew = 0;
}
}
这是日志文件的样子
*TRANSACTION STARTED*
[020t CARD INSERTED
[020tCARD: *************5845
DATE 01-02-16 TIME 05:45:52
05:46:26 GENAC 1 : ARQC
EXTERNAL AUTHENTICATE: NO ARPC
05:46:30 GENAC 2 : AAC
01 FEB 2016 05:47:41 10160021
WITHDRAW
FROM XXXXXXXX ?
INVALID TRANSCATION ON TERMINAL.
-----------------------
[020t 05:47:05 CARD TAKEN
[020t 05:47:07 TRANSACTION END
[0r(1)2[000p[040qe1w3h162[020t*742*02/01/2016*05:47*
*TRANSACTION STARTED*
[020t CARD INSERTED
[020tCARD: *************2584
DATE 01-02-16 TIME 05:47:27
05:48:00 GENAC 1 : ARQC
05:48:05 GENAC 2 : TC
[020t 05:48:16 CARD TAKEN
[020t 05:48:22 NOTES PRESENTED 0,0,2,0
01 FEB 2016 05:48:52 10160021
WITHDRAW
FROM XXXXXXXX ?
AMT GHC40.00
[020t 05:48:31 TRANSACTION END
[0r(1)2[000p[040qe1w3h162[020t*743*02/01/2016*05:57*
在每个.txt文件中,Desired输出应如下所示
*TRANSACTION STARTED*
[020t CARD INSERTED
[020tCARD: *************2584
DATE 01-02-16 TIME 05:47:27
05:48:00 GENAC 1 : ARQC
05:48:05 GENAC 2 : TC
[020t 05:48:16 CARD TAKEN
[020t 05:48:22 NOTES PRESENTED 0,0,2,0
01 FEB 2016 05:48:52 10160021
WITHDRAW
FROM XXXXXXXX ?
AMT GHC40.00
[020t 05:48:31 TRANSACTION END
答案 0 :(得分:0)
当我们看到开始标记时,我们设置了一个标志,当我们看到结束标记时我们关闭标记。如果启用该标志,我们打印行。您必须根据您的情况调整路径,但此代码肯定会创建顺序文件,每个事务一个。
use strict;
use warnings;
my $count = 0;
my $spew = 0;
my $out;
while (<DATA>) {
if (/TRANSACTION STARTED/) {
open($out, '>', "TRANSACTION$count.txt") or die $!;
$spew = 1;
$count++;
}
if ($spew) {
print $out $_;
}
if (/TRANSACTION END/) {
close($out);
$spew = 0;
}
}
__DATA__
*TRANSACTION STARTED*
[020t CARD INSERTED
[020tCARD: *************5845
DATE 01-02-16 TIME 05:45:52
05:46:26 GENAC 1 : ARQC
EXTERNAL AUTHENTICATE: NO ARPC
05:46:30 GENAC 2 : AAC
01 FEB 2016 05:47:41 10160021
WITHDRAW
FROM XXXXXXXX ?
INVALID TRANSCATION ON TERMINAL.
-----------------------
[020t 05:47:05 CARD TAKEN
[020t 05:47:07 TRANSACTION END
[0r(1)2[000p[040qe1w3h162[020t*742*02/01/2016*05:47*
*TRANSACTION STARTED*
[020t CARD INSERTED
[020tCARD: *************2584
DATE 01-02-16 TIME 05:47:27
05:48:00 GENAC 1 : ARQC
05:48:05 GENAC 2 : TC
[020t 05:48:16 CARD TAKEN
[020t 05:48:22 NOTES PRESENTED 0,0,2,0
01 FEB 2016 05:48:52 10160021
WITHDRAW
FROM XXXXXXXX ?
AMT GHC40.00
[020t 05:48:31 TRANSACTION END
[0r(1)2[000p[040qe1w3h162[020t*743*02/01/2016*05:57*
答案 1 :(得分:-1)
嗯,据我所知,您必须在遇到“交易开始”时打开一个文件。当你遇到&#39; TRANSACTION END&#39;时关闭它。所以你应该重写你的循环。这样的事情:
while (<$fh>) {
if (/TRANSACTION STARTED/) {
$outputfile= "${outdir}TRANSACTION$filenumber";
open ($ofh, "> $outputfile") or die "Could not open file '$outfile' $!";
print $ofh $_;
} elsif (/TRANSACTION END/) {
print $ofh $_;
close $ofh;
$filenumber++;
} else {
print $ofh $_;
}
}