Avatar of sparker1970
sparker1970
Flag for United States of America asked on

Parse a Text File Based on Record Number in File

I have a fixed length file I receive from a client that contains 12 potential record types. The records are identified by a 3 digit number in positions 33-35 called a Transaction-Code.

What I need is a batch program that can split the file by Transaction-Code and output the entire record as a text file:

Transaction-Code / File Output
110 / Record110.txt
111 / Record111.txt
210 / Record210.txt
211 / Record211.txt
213 / Record213.txt
214 / Record214.txt
300 / Record300.txt
400 / Record400.txt
700 / Record700.txt
800 / Record800.txt
900 / Record900.txt
910 / Record910.txt
ParseSample.txt
Windows Batch

Avatar of undefined
Last Comment
sparker1970

8/22/2022 - Mon
[ fanpages ]

What I need is a batch program...

Topics applied: Visual Basic Classic, .NET Programming, Windows Batch
Topics removed: Visual Basic Classic, .NET Programming
Kimputer

Try this VB code (assumes you were open to VB besides a batch program)?

Imports System.IO

Module Module1

    Dim inputfile As String = "C:\Users\Kimputer\Documents\transactioncode.txt"
    Dim outputpath As String = "C:\Users\Kimputer\Documents"
    Sub Main()
        Dim sr As New StreamReader(inputfile)

        Do Until sr.EndOfStream
            Dim line = sr.ReadLine
            Dim number = (Left(Mid(line, 33), 3))
            Dim sw As New StreamWriter(Path.Combine(outputpath, "Record" & number.ToString & ".txt"))
            sw.WriteLine(line)
            sw.Close()
            sw = Nothing
        Loop
    End Sub

End Module

Open in new window

Kimputer

Here's the DOS batch file variant (add fixed paths yourself, otherwise you need to run it where all the source AND target files should be):

@echo off
for /f "tokens=*" %%a in (transactioncode.txt) do call :processline %%a

goto :eof

:processLine
set str=%2
set str=%str:~0,3%
echo %1 %2 > Record%str%.txt
goto :eof

:eof

Open in new window

Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
it_saige

I would simply do something like:
@echo off
SETLOCAL DisableDelayedExpansion
FOR /F "usebackq delims=" %%a in (`"findstr /n ^^ test.txt"`) do (
	set "line=%%a"
	call :processLine line
)
goto :eof

:processLine
SETLOCAL EnableDelayedExpansion
set "line=!%1!"
set "line=!line:*:=!"
echo %line:~35% >> Record%line:~32,3%.txt
ENDLOCAL
goto :eof

Open in new window


Which from your example document:Capture.JPGProduces:Capture.JPGThe contents of each file are the sequence of characters after the 35th column; e.g. -Capture.JPG
-saige-
sparker1970

ASKER
Kimputer - the batch script you provided is only pulling back single records...if I have 5 records with "300" in the position  am looking at, I need all 5 in the output. The batch syntax provided only outputs a single record to each output file.

I may have hundreds of records so I need all records with the identifier in each text file

it_saige - I'm not sure what your batch file is doing. I am not getting the whole record and it is nothing like the original record
ASKER CERTIFIED SOLUTION
it_saige

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
it_saige

I just did another test on the file you specified and added adding additional lines with the same markers specified.

Just for clarification, you have a file that is like what you stated, inside this file are individual lines that have a maker starting on the 33rd column and ending on the 35th column.  This marker specifies a 'Transaction-Code'.  You want to take this transaction code and write the line data (you need to specify if the line data is the full line or just the data after the 35th column) to a file that is called Record<Transaction-Code>.txt; e.g. Transaction-Code 210 = Record210.txt.  Am I right so far?

However, if Kimputer's works fine as far as parsing the correct data, then you simply need to change their script to this:
@echo off
for /f "tokens=*" %%a in (transactioncode.txt) do call :processline %%a

goto :eof

:processLine
set str=%2
set str=%str:~0,3%
echo %1 %2 >> Record%str%.txt
goto :eof

:eof

Open in new window

Use the '>>' in the echo line as opposed to '>'.
> is used to overwrite.
>> is used to append.

-saige-
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
sparker1970

ASKER
This worked perfectly and maintained the integrity of the record. Very easy solution to implement. Thank you very Much!!!