Solved

Split 1 Text File into several files

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
topping1 challenge 7 110
VB.net and sql server 4 45
Select specific duplicate row based on specific criteria 4 55
storing csv file in table variable in Python 2 23
I know it’s not a new topic to discuss and it has lots of online contents already available over the net. But Then I thought it would be useful to this site’s visitors and can have online repository on vim most commonly used commands. This post h…
This article will inform Clients about common and important expectations from the freelancers (Experts) who are looking at your Gig.

809 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