Reputation: 21
I have .txt files (1000's) that look similar to the below example. I MUST do this in a Dos.bat file, no options (it will be called by other .bat files) (running windows 7 with no option to install or download any other files or programs). The files can be generally 30k in size. I need to replace the $'s characters with a carriage return. There will be as many as 2-3 hundred $'s characters in each file so I cannot parse out the $'s (unless I am missing something).
It seems so simple, but I have searched diligently trying to do this. I found an example somewhere, but it appears to be limited to 8k max. files. (see end of question)
The patterns of commas and words inside the files change with each file. The only constant is the $ where there should be a new line. Example:
ORIGINAL FILE:
ENTRY,1,423-6823,"FILTER ASSEMBLY, DAVCO",A,,1,2,,,,,,@FIELD,SUGG_QTY,1$ENTRY,2,423-6939,"FILTER, FUEL - SPIN-ON",A,,2,3,,,,,,@FIELD,SUGG_QTY,1$ENTRY,3,423-3143,"MOUNT PLATE, FILTER ASSEMBLY",1,,1,4,,,,,,$ENTRY,4,698-9393,"HOSE ASSEMBLY, #16 X 112""",1,,1,5,,,,,,$ENTRY,5,418-6146,"HOSE ASSEMBLY, #16 X 118""",1,,1,6,,,,,,$ENTRY,6,408-2841,"HOSE ASSEMBLY, #16 X 40""",1,,1,7,,,,,,$ENTRY,7,412-7996,"HOSE ASSEMBLY, #12 X 40""",1,,1,8,,,,,,$ENTRY,8,400-2483,"ELBOW, #16 FJ X 90 deg. X #16 MJ - SWIVEL",3,,1,9,,,,,,$ENTRY,9,N.P.N.,FILTER MOUNT [FURNISHED WITH ENGINE],A,,1,10,,,,,,$ENTRY,10,423-6822,"FILTER, FUEL",A,,1,11,,,,,,@FIELD,SUGG_QTY,2$ENTRY,11,400-2481,"ELBOW, #12 FJ X 90 deg. X #12 MJ - SWIVEL",1,,1,12,,,,,,
and so on, maybe 30-40k in size.
The file should look like this when the $'S are removed and replaced with a carriage return in txt file:
ENTRY,1,423-6823,"FILTER ASSEMBLY, DAVCO",A,,1,2,,,,,,@FIELD,SUGG_QTY,1
ENTRY,2,423-6939,"FILTER, FUEL - SPIN-ON",A,,2,3,,,,,,@FIELD,SUGG_QTY,1
ENTRY,3,423-3143,"MOUNT PLATE, FILTER ASSEMBLY",1,,1,4,,,,,,
ENTRY,4,698-9393,"HOSE ASSEMBLY, #16 X 112""",1,,1,5,,,,,,
ENTRY,5,418-6146,"HOSE ASSEMBLY, #16 X 118""",1,,1,6,,,,,,
ENTRY,6,408-2841,"HOSE ASSEMBLY, #16 X 40""",1,,1,7,,,,,,
ENTRY,7,412-7996,"HOSE ASSEMBLY, #12 X 40""",1,,1,8,,,,,,
ENTRY,8,400-2483,"ELBOW, #16 FJ X 90 deg. X #16 MJ - SWIVEL",3,,1,9,,,,,,
ENTRY,9,N.P.N.,FILTER MOUNT [FURNISHED WITH ENGINE],A,,1,10,,,,,,
ENTRY,10,423-6822,"FILTER, FUEL",A,,1,11,,,,,,@FIELD,SUGG_QTY,2
ENTRY,11,400-2481,"ELBOW, #12 FJ X 90 deg. X #12 MJ - SWIVEL",1,,1,12,,,,,,
I can do it on small files with the following code (found this on some site). it works perfectly, BUT when the file size reaches about 9k it fails and only returns the first line.
For /f "tokens=1,* delims=$" %%a in (testfile.txt) Do (
Echo.%%a>>out.txt
If not "%%b"=="" Call :change %%b)
GoTo :EOF
:change
For /f "tokens=1,* delims=$" %%a in ("%*") Do (
Echo.%%a>>out.txt
If not "%%b"=="" Call :change %%b)
GoTo :EOF
any help is greatly appreciated.
Upvotes: 2
Views: 7219
Reputation: 67196
You may read the file in blocks of 1 KB size with SET /P
command, accumulate 2 blocks and divide the resulting block in lines at "$" character; however, this method may be somewhat slow if the files are large.
@echo off
setlocal EnableDelayedExpansion
set nextBlock=0
set "thisBlock="
:nextBlock
rem Read the next block of characters
set /A nextBlock+=1
(for /L %%i in (1,1,%nextBlock%) do (
set "block="
set /P "block="
)) < input.txt
if not defined block goto endFile
rem Append next block to last part of previous block
set "thisBlock=!thisBlock!!block!"
rem Process current block: separate lines at "$" character
:nextLine
for /F "tokens=1* delims=$" %%a in ("!thisBlock!") do (
set "nextLine=%%a"
set "thisBlock=%%b"
)
if defined thisBlock (
echo !nextLine!
goto nextLine
)
set "thisBlock=!nextLine!"
goto nextBlock
:endFile
if defined thisBlock echo !thisBlock!
Upvotes: 0
Reputation: 67196
You may use my FindRepl.bat program to do that. For example:
> type input.txt
ENTRY,1,423-6823,"FILTER ASSEMBLY, DAVCO",A,,1,2,,,,,,@FIELD,SUGG_QTY,1$ENTRY,2,
423-6939,"FILTER, FUEL - SPIN-ON",A,,2,3,,,,,,@FIELD,SUGG_QTY,1$ENTRY,3,423-3143
,"MOUNT PLATE, FILTER ASSEMBLY",1,,1,4,,,,,,$ENTRY,4,698-9393,"HOSE ASSEMBLY, #1
6 X 112""",1,,1,5,,,,,,$ENTRY,5,418-6146,"HOSE ASSEMBLY, #16 X 118""",1,,1,6,,,,
,,$ENTRY,6,408-2841,"HOSE ASSEMBLY, #16 X 40""",1,,1,7,,,,,,$ENTRY,7,412-7996,"H
OSE ASSEMBLY, #12 X 40""",1,,1,8,,,,,,$ENTRY,8,400-2483,"ELBOW, #16 FJ X 90 deg.
X #16 MJ - SWIVEL",3,,1,9,,,,,,$ENTRY,9,N.P.N.,FILTER MOUNT [FURNISHED WITH ENG
INE],A,,1,10,,,,,,$ENTRY,10,423-6822,"FILTER, FUEL",A,,1,11,,,,,,@FIELD,SUGG_QTY
,2$ENTRY,11,400-2481,"ELBOW, #12 FJ X 90 deg. X #12 MJ - SWIVEL",1,,1,12,,,,,,
> FindRepl "\$" "\r\n" < input.txt
ENTRY,1,423-6823,"FILTER ASSEMBLY, DAVCO",A,,1,2,,,,,,@FIELD,SUGG_QTY,1
ENTRY,2,423-6939,"FILTER, FUEL - SPIN-ON",A,,2,3,,,,,,@FIELD,SUGG_QTY,1
ENTRY,3,423-3143,"MOUNT PLATE, FILTER ASSEMBLY",1,,1,4,,,,,,
ENTRY,4,698-9393,"HOSE ASSEMBLY, #16 X 112""",1,,1,5,,,,,,
ENTRY,5,418-6146,"HOSE ASSEMBLY, #16 X 118""",1,,1,6,,,,,,
ENTRY,6,408-2841,"HOSE ASSEMBLY, #16 X 40""",1,,1,7,,,,,,
ENTRY,7,412-7996,"HOSE ASSEMBLY, #12 X 40""",1,,1,8,,,,,,
ENTRY,8,400-2483,"ELBOW, #16 FJ X 90 deg. X #16 MJ - SWIVEL",3,,1,9,,,,,,
ENTRY,9,N.P.N.,FILTER MOUNT [FURNISHED WITH ENGINE],A,,1,10,,,,,,
ENTRY,10,423-6822,"FILTER, FUEL",A,,1,11,,,,,,@FIELD,SUGG_QTY,2
ENTRY,11,400-2481,"ELBOW, #12 FJ X 90 deg. X #12 MJ - SWIVEL",1,,1,12,,,,,,
FindRepl.bat program have no limit in the file size and run very fast; you may download it from this site
Upvotes: 1
Reputation: 4784
Try using Windows PowerShell. This will give you full scripting capability but is built into Windows.
Upvotes: 0