Link to home
Start Free TrialLog in
Avatar of lucour
lucour

asked on

Split 1 Text File into several files

Hi,

I have the following text file that contains validation information from files that were uploaded to a bank.

1 ORG - TDMARxxxxx1 FCDT - 04/08/18 FCNO - xxyy TC - dyyy                        
3 RECVD : NO -      619 AMT -        nnnnn.nn                                  
4 SBMTD : NO -      619 AMT -        nnnnnn.nn                                  
5 REJECT: NO -        0 AMT -            0.00                                  
1 ORG - TDMARxxxxx2 FCDT - 04/08/18 FCNO - xxyy TC - dyyy                        
3 RECVD : NO -      619 AMT -        nnnnn.nn                                  
4 SBMTD : NO -      619 AMT -        nnnnnn.nn                                  
5 REJECT: NO -        0 AMT -            0.00  
1 ORG - TDMARxxxxx1 FCDT - 04/08/18 FCNO - xxyy TC - dyyy                        
3 RECVD : NO -      619 AMT -        nnnnn.nn                                  
4 SBMTD : NO -      619 AMT -        nnnnnn.nn                                  
5 REJECT: NO -        0 AMT -            0.00  

My problem ?  Three different accounts are concatenated into this 1 file.  I would like to split this into 3 seperate files.  Each new account starts at the "1 ORG" string.  So, first occurence of "1 ORG" writes to a new text file called FileA.txt, the second occurence writes to a text file called FileB.txt ......... and so on.

Thanks !
Avatar of griessh
griessh
Flag of United States of America image

Hi lucour,

What are your programming options, what is your OS?

======
Werner
Avatar of anilmane
anilmane

If you are interestd in VB.net using C#
I can help you.....

Thanks
Anil Mane....
ASKER CERTIFIED SOLUTION
Avatar of jmacmicking
jmacmicking
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
# Solution in Perl:
# first slurps the file into a variable
# next parses the file into an array, taking every item that starts with '1 ORG',
# ...but only if it is followed by either '1 ORG' or the end of the file
# then writes out the parts into files of their own
use strict;

open(FILE, 'foo.txt');
my $file = join('', <FILE>);
close FILE ;

my @parts = ($file =~ /(^1 ORG(?:.|\s)*?)(?=^1 ORG|\Z)/mg);

for(my $x = 0; $x < (@parts); $x++) {
   open(FILE, ">out.$x");
   print FILE $parts[$x];
   close FILE;
}
Avatar of lucour

ASKER

Sorry, all !  I forgot the mention what language I was using.  It is VBScript.  Based on this,  jmacmicking I tried your solution and this worked perfectly.  Thanks to you, and to all, for your help.