CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Extract ranges of lines from a file, saving each range to a separate file

 



atoivan
New User

Feb 23, 2016, 11:58 PM

Post #1 of 2 (1056 views)
Extract ranges of lines from a file, saving each range to a separate file Can't Post

I have a file that looks like this


Code
     *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*


and need to extract what is between *TRANSACTION STARTED* and TRANSACTION END, ignoring all other information, and create a new file for each range.
The new file will contain only


Code
[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


this is What I have but it is not working it dose not save the files in the folder


Code
#! /usr/bin/perl/  

use warnings;
use strict;

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";

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)',$outputfile))
{
print " $outputfile worked $!";
}else
{
die "Could not write to $outputfile $!";
}


close ($outputfile);



BillKSmith
Veteran

Feb 24, 2016, 6:00 AM

Post #2 of 2 (1042 views)
Re: [atoivan] Extract ranges of lines from a file, saving each range to a separate file [In reply to] Can't Post

You must explicitly open each output file. The following code should give you the idea:

Code
#! /usr/bin/perl/   

use warnings;
use strict;

my $outdir = "/home/lord-ivan/Soures_Code/Perl/projects/EJ Transport/Queue/";
my $somefile = "/home/lord-ivan/Soures_Code/Perl/projects/Data/EJDATA.LOG";
my $ofh;
my $filenumber = 0;

open (my $fh, '<:encoding(UTF-8)', $somefile) or die "Could not open file '$somefile' $!";
print "$somefile open\n";
my $outputfile= "${outdir}TRANSACTION$filenumber";
open ($ofh, '>:encoding(UTF-8)',$outputfile);
print "$outputfile open\n";

while (<$fh>) {

if (/TRANSACTION STARTED/ .. /TRANSACTION END/)
{
next if /TRANSACTION\s*(STARTED|END)/;
print {$ofh} $_;
next;
}
close $ofh;
$filenumber++;
$outputfile= "${outdir}TRANSACTION$filenumber";
open ($ofh, '>>:encoding(UTF-8)',$outputfile) or die "Cannot open $outputfile:$~";
}

close $ofh;
close $fh;


Note: This is not production code. I did not make any changes not directly related to your problem. The output files do not contain the final line shown in your example. I have never used UTF, so I am not certain that your open statements are correct. If the last line of your input is not an 'END' line, your final file will be incomplete (or empty).
Good Luck,
Bill

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives