Solved

Split 1 Text File into several files

Posted on 2004-08-19
5
261 Views
Last Modified: 2010-04-17
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 !
0
Comment
Question by:lucour
5 Comments
 
LVL 11

Expert Comment

by:griessh
ID: 11842810
Hi lucour,

What are your programming options, what is your OS?

======
Werner
0
 
LVL 2

Expert Comment

by:anilmane
ID: 11842829
If you are interestd in VB.net using C#
I can help you.....

Thanks
Anil Mane....
0
 
LVL 5

Accepted Solution

by:
jmacmicking earned 125 total points
ID: 11843040
Outline:
1  Open Input file
2  Read in until a line starting with "1 ORG" is reached
3  Create new output file
4  Write every line to output file until a line with "1 ORG" the EOF is reached.  Repeat step 3 if not EOF.  If EOF, exit.

In VBScript (just save to a text file with .vbs as the extension):

      Dim InputFile, OutputFile
      Dim FileCount
      Dim CurLine, FileName
      Dim FSO
      
      Set FSO = CreateObject("Scripting.FileSystemObject")
      Set InputFile = FSO.OpenTextFile("YourFile.txt", 1)
      FileCount = 0
      
      While Not Left(CurLine, 5)="1 ORG" And Not InputFile.AtEndOfStream
        CurLine = InputFile.ReadLine
      Wend
      
      If InputFile.AtEndOfStream Then WScript.Quit
      
      While Not InputFile.AtEndOfStream
        If  Left(CurLine, 5)="1 ORG" Then
          If IsObject(OutputFile) Then OutputFile.Close
          Set OutputFile = Nothing
          OFileName = "File" & Chr(Asc("A") + FileCount) & ".txt"
          Set OutputFile = FSO.CreateTextFile(OFileName , True)
          FileCount = FileCount + 1
        End If
        OutputFile.WriteLine CurLine
      
        CurLine = InputFile.ReadLine
      Wend
      
      OutputFile.Close
      InputFile.Close
      WScript.Quit
0
 
LVL 1

Expert Comment

by:andrewtn1
ID: 11843060
# 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;
}
0
 

Author Comment

by:lucour
ID: 11843562
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.
0

Featured Post

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A short article about problems I had with the new location API and permissions in Marshmallow
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question