Solved

Split 1 Text File into several files

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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

707 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now