파일에서 줄 범위를 추출하여 각 범위를 별도의 파일에 저장

파일에서 줄 범위를 추출하여 각 범위를 별도의 파일에 저장

다음과 같은 파일이 있습니다

740*02/01/2016*00:00*
EJ LOG COPIED OK

AUTO INIT COPY DRIVE NOT CONFIGURED

E1EF3901
[020t*741*02/01/2016*05:45*
     *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

이게 내가 가진 거야

    #! /usr/bin/perl/ -w

print "Content-type: text/html\n\n";

use strict;


my $somefile = "/home/lord-ivan/Soures_Code/Perl/projects/Data/EJDATA.LOG";

if(open (my $fh, '<:encoding(UTF-8)', $somefile))
{
    print " $somefile is opened   $!";
}else
{
    die "Could not open file '$somefile' $!";
}

while (<$fh>) {

    if (/TRANSACTION STARTED/ .. /TRANSACTION END/) 
{
     next if /TRANSACTION\s*(STARTED|END)/;
    print $_;   
}
}

close ($somefile);



my $outputfile = "/home/lord-ivan/Soures_Code/Perl/projects/EJ Transport/Queue/";

if(open (my $ofh, '>>:encoding(UTF-8)',print $ofh $outputfile))
{
    print " $outputfile worked   $!";
}else
{
    die "Could not write to  $outputfile  $!";
}


close ($outputfile);

답변1

다음은 파일을 열고 내용을 쓰는 빠른 스크립트입니다. 트리거 연산자의 반환 값을 사용하여 첫 번째 행(값은 1)인지 마지막 행(값은 "E0"으로 끝남)인지 확인합니다.

use strict;
use warnings;

my $file = "a001";
my $fh;

while (<>) {
    my $l = /start/ .. /stop/; 
    if ($l && $l == 1) {
        open $fh, ">", $file++ or die "Cannot open file"
    } elsif ($l && $l !~ /E0$/) { 
        print $fh $_
    }
}

답변2

awk의 경우 in.log는 내용이 포함된 입력 파일입니다.

cat in.log | awk '/TRANSACTION STARTED/{getline;filenum++;print " ">filenum".out";f=1;}; /TRANSACTION END/{gsub(/TRANSACTION END/,"");print $0>>filenum".out";f=0} ; {if(f==1){print $0>>filenum".out";};}'

1.out부터 시작하여 각 파일을 만듭니다.

[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 

답변3

나는 당신처럼 트리거 연산자를 사용하는 대신 이 작업을 수행합니다.

#!/usr/bin/perl

use warnings;
use strict;
#set record separator
local $/ = 'TRANSACTION END'; 
#output file starts number 0. 
my $output_file_count = 0; 
#iterate filehandle - <> is the magic FH, so reads STDIN or files 
#specified as args to the script. 
while ( <> ) { 
    #discard anything before 'TRANSACTION STARTED'
    s/.*\*TRANSACTION STARTED\*\s*\n//ms;
    #skip unless there's an 'END' here (so trailing junk gets discarded)
    next unless m/TRANSACTION END/; 
    #open a new output file. 
    open ( my $output, '>', "transaction_".$output_file_count++.".log" ) or die $!;
    #set it as the location to print by default
    select $output; 
    #print this record (to $output, because of select)
    print; 
    #close it
    close ( $output );
}

답변4

이 문장은 문제를 해결해야 합니다:

$ perl -ne 'BEGIN{$fname=0};if ((/TRANSACTION STARTED/ .. /TRANSACTION END/) && $_ !~ /TRANSACTION\s*(STARTED|END)/){open FILE, ">>${fname}.txt";print FILE $_;}else{close($fname);$fname++}' file

파일 이름은 "txt" 접미사가 붙은 숫자일 뿐입니다. 내 출력은 다음과 같습니다

$ head -50 [0-9]*.txt
==> 11.txt <==
[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

==> 14.txt <==
[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

==> 17.txt <==
[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

==> 8.txt <==
[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

관련 정보