Solved

Split 1 Text File into several files

Posted on 2004-08-19
5
263 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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.
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

680 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