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
sparker1970Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

[ fanpages ]IT Services ConsultantCommented:
What I need is a batch program...

Topics applied: Visual Basic Classic, .NET Programming, Windows Batch
Topics removed: Visual Basic Classic, .NET Programming
0
KimputerCommented:
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

0
KimputerCommented:
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

0
CompTIA Security+

Learn the essential functions of CompTIA Security+, which establishes the core knowledge required of any cybersecurity role and leads professionals into intermediate-level cybersecurity jobs.

it_saigeDeveloperCommented:
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-
0
sparker1970Author Commented:
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
0
it_saigeDeveloperCommented:
Is the whole record the line itself?

If it is then you need to change the batch to this:
@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% >> Record%line:~32,3%.txt
ENDLOCAL
goto :eof

Open in new window


-saige-
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
it_saigeDeveloperCommented:
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-
0
sparker1970Author Commented:
This worked perfectly and maintained the integrity of the record. Very easy solution to implement. Thank you very Much!!!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Windows Batch

From novice to tech pro — start learning today.