批处理文件替换文本文件中的字符

时间:2013-09-29 02:40:14

标签: batch-file

我有.txt文件(1000),看起来类似于下面的例子。我必须在Dos.bat文件中执行此操作,没有选项(它将被其他.bat文件调用)(运行Windows 7,没有选项来安装或下载任何其他文件或程序)。文件大小通常为30k。我需要用回车代替$的字符。每个文件中将有多达2-3百美元的字符,因此我无法解析$(除非我遗漏了某些内容)。

看起来很简单,但我一直在努力尝试这样做。我在某个地方找到了一个例子,但它似乎限制在最大8k。文件。 (见问题末尾)

文件中的逗号和单词模式随每个文件而变化。唯一不变的是$应该有一条新线。例如:

原始文件:

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,,,,,,

等等,大小可能是30-40k。

当$'S被移除并替换为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,,,,,,

我可以使用以下代码在小文件上执行此操作(在某些站点上找到此文件)。它完美地工作,但是当文件大小达到大约9k时它失败并且只返回第一行。

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

非常感谢任何帮助。

3 个答案:

答案 0 :(得分:1)

您可以使用我的FindRepl.bat程序来执行此操作。例如:

> 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程序对文件大小没有限制并且运行速度非常快;您可以从this site

下载

答案 1 :(得分:0)

尝试使用Windows PowerShell。这将为您提供完整的脚本功能,但内置于Windows中。

答案 2 :(得分:0)

您可以使用SET /P命令以1 KB大小的块读取文件,累积2个块并将生成的块除以“$”字符的行;但是,如果文件很大,这种方法可能会有些慢。

@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!