Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Feb 24th, 2016, 1:33 AM   #1
atoivan
Newbie
 
Join Date: Nov 2010
Posts: 7
Rep Power: 0 atoivan is an unknown quantity at this point
Extract ranges of lines from a file, saving each range to a separate file

I have a file that looks like this

     *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

[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

#! /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);
atoivan is offline   Reply With Quote
Reply

Bookmarks

Tags
perl, regex

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Using sed to copy first line in file then append to all lines zabbix Sed and Awk 6 Nov 24th, 2010 1:35 PM
saving a file using c# in Wpf? JerryRude C# 1 Aug 14th, 2009 3:35 PM
How much can a C++ programme extract from a .txt file? Ade C++ 14 May 4th, 2006 6:46 AM
having problem with data not saving to a file codylee270 C++ 5 Apr 13th, 2006 7:39 PM
removing certain lines from a file Dizzutch Perl 9 Jan 26th, 2005 9:55 AM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 8:45 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC