我有一个看起来像这样的文件
*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*
*TRANSACTION STARTED*
[020t CARD INSERTED
[020tCARD: *************3862
DATE 01-02-16 TIME 05:57:28
01 FEB 2016 05:58:33 10160021
INQUIRY
FROM XXXXXXXX90018
AVAIL GHC1260.20
LEDGER GHC1260.20
[020t 05:58:06 CARD TAKEN
[020t 05:58:11 TRANSACTION END
[0r(1)2[000p[040qe1w3h162[020t*744*02/01/2016*06:43*
*TRANSACTION STARTED*
[020t CARD INSERTED
[020tCARD: *************1972
DATE 01-02-16 TIME 06:43:53
01 FEB 2016 06:44:56 10160021
5029110111271972
4490 4490
INQUIRY
FROM XXXXXXXX23013
AVAIL GHC14.28
LEDGER GHC14.28
[020t 06:44:25 CARD TAKEN
[020t 06:44:29 TRANSACTION END
[0r(1)2[000p[040qe1w3h162[020t*745*02/01/2016*06:56*
并且需要提取*TRANSACTION STARTED*
和TRANSACTION END
之间的内容,忽略所有其他信息,并为每个范围创建一个新文件。
新文件仅包含
[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
我对之前做了一些修改,但是我得到了这个错误:
Can't use string ("/home/lord-ivan/Soures_Code/Perl"...) as a SCALAR ref while "strict refs" in use at readfile.pl line 31, <$fh> line 1.
我的新代码
#! /usr/bin/perl/
use warnings;
use strict;
#file to open
my $somefile = "/home/lord-ivan/Soures_Code/Perl/projects/Data/EJDATA.LOG";
open (my $fh, '<:encoding(UTF-8)', $somefile) or die "Could not open file '$somefile' $!";
print "$somefile open";
#Extract ranges of lines from a file
while (<$fh>) {
if (/TRANSACTION STARTED/ .. /TRANSACTION END/)
{
next if /TRANSACTION\s*(STARTED|END)/;
print $_;
}
}
my $ofh;
my $outputfile = "/home/lord-ivan/Soures_Code/Perl/projects/EJ Transport/Queue/.txt";
BEGIN {$outputfile= "EJ"};
open ($fh, ">>${$outputfile}.txt", print $fh $_);
close($outputfile);
$outputfile++;
为什么它不起作用。
答案 0 :(得分:0)
@ IORD-伊凡 尝试学习
什么是数据类型。?
文件处理
什么是处理程序。
您的$somefile
仅存储文件名。然后使用$somefile
迭代while循环。但是您的$somefile
仅包含不包含文件内容的文件名。
您的文件内容存储在$fh
处理程序中,因此迭代$ fh处理程序的while循环。然后逐行检查。
use warnings;
use strict;
my $somefile = "input.txt";
open (my $fh, '<:encoding(UTF-8)', $somefile) or die "Could not open file '$somefile' $!";
print "$somefile open";
while (<$fh>) {
if (/TRANSACTION STARTED/ .. /TRANSACTION END/)
{
next if /TRANSACTION\s*(STARTED|END)/;
print $_;
}
}
学习perl后,完成平衡程序(FILE WRITE)。 :)