Solved

Split 1 Text File into several files

Posted on 2004-08-19
5
266 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
Part One of the two-part Q&A series with MalwareTech.
Introduction to Processes
Starting up a Project

630 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