Saturday, 28 September 2013

batch file to replace a character in a txt file

batch file to replace a character in a txt file

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 (without the extra line between each
line--it would not let me post without that extra line):
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.

No comments:

Post a Comment