2

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

看起来很简单,但我一直在努力搜索试图做到这一点。我在某处找到了一个示例,但它似乎限制为最大 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

任何帮助是极大的赞赏。

4

3 回答 3

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程序对文件大小没有限制,运行速度非常快;你可以从这个网站下载

于 2013-09-29T03:37:47.573 回答
0

您可以使用命令以 1 KB 大小的块读取文件SET /P,累积 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!
于 2013-09-29T06:48:34.030 回答
0

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

于 2013-09-29T02:43:52.180 回答