@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
:: remove variables starting $ or #
For %%b IN ($ #) DO FOR /F "delims==" %%a In ('set %%b 2^>Nul') DO SET "%%a="
:: load $*=IDs, #*=names
SET /a count=0
FOR /f "tokens=1*delims=:" %%a IN ('findstr /n /r ".*" q27679364u.txt') DO SET "$%%a=%%b"
FOR /f "tokens=1*delims=:" %%a IN ('findstr /n /r ".*" q27679364n.txt') DO SET "#%%a=%%b"&SET /a count+=1
(
FOR /f "delims=" %%a IN (q27679364d.txt) DO (
SET "line=%%a"
CALL :process
)
)>"newfile.txt"
GOTO :EOF
:process
FOR /l %%i IN (1,1,%count%) DO CALL :SUBST "%%$%%i%%" "%%#%%i%%"
ECHO(%line%
GOTO :eof
:SUBST
CALL SET "line=%%line:%~1=%~2%%
GOTO :eof
我用名为q27679364u.txt
的文件包含您的ID数据和q27679364n.txt
您的姓名数据用于我的测试。
主要生产newfile.txt
有了这个输入数据文件q27679364d.txt
:
substitute here: 001_Blue019
nothing to substitute
what about this? 002_Bluer11 and 003_Buster142 and 001_Blue019
--- now your data ----
001_Blue019
001_Blue019
001_Blue019
002_Bluer11
001_Blue019
001_Blue019
003_Buster142
(我做了我自己的数据文件,而我在等待,然后添加你的末数据)
结果是:
substitute here: Bob Blue
nothing to substitute
what about this? Bluer Baxster and Buster Arnold and Bob Blue
--- now your data ----
Bob Blue
Bob Blue
Bob Blue
Bluer Baxster
Bob Blue
Bob Blue
Buster Arnold
从而出现了newfile.txt
附录。
批量并不知道其速度,但可以通过调整例程来完成大量工作,尤其是根据正在处理的数据的特性。
通过对OP数据进行大量复制,我将数据文件中的行数扩展到10,000以上,并测量了上述过程。在我的机器花176秒(实际时间将取决于每个文件的机器的特性和尺寸。)
我然后修改的程序中,假设在第三文件中的数据包含从ID文件只线,以随机顺序并可能重复。
这个结果:
@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
:: remove variables starting $ or #
For %%b IN ($ #) DO FOR /F "delims==" %%a In ('set %%b 2^>Nul') DO SET "%%a="
:: load $*=IDs, #*=names
SET /a count=0
FOR /f "tokens=1*delims=:" %%a IN ('findstr /n /r ".*" q27679364u.txt') DO SET "$%%a=%%b"
FOR /f "tokens=1*delims=:" %%a IN ('findstr /n /r ".*" q27679364n.txt') DO SET "#%%a=%%b"&SET /a count+=1
(
FOR /f "delims=" %%a IN (q27679364d.txt) DO (
FOR /f "tokens=1*delims=$=" %%i IN ('set $') DO IF /i "%%j"=="%%a" ECHO !#%%i!
)
)>"newfile.txt"
GOTO :EOF
跑109秒 - 一个有用的保留。
所以我想了更多。使用相同的数据,我开发了这个:
@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
:: remove variables starting $ or # or _
For %%b IN ($ # _) DO FOR /F "delims==" %%a In ('set %%b 2^>Nul') DO SET "%%a="
:: load $*=IDs, #*=names
SET /a count=0
FOR /f "tokens=1*delims=:" %%a IN ('findstr /n /r ".*" q27679364u.txt') DO SET "$%%a=%%b"
FOR /f "tokens=1*delims=:" %%a IN ('findstr /n /r ".*" q27679364n.txt') DO SET "#%%a=%%b"&SET /a count+=1
FOR /L %%a IN (1,1,%count%) DO SET "_!$%%a!=!#%%a!"&SET "$%%a="&SET "#%%a="
(
FOR /f "delims=" %%a IN (q27679364d.txt) DO (ECHO !_%%a!
)
)>"newfile.txt"
GOTO :EOF
相同结果文件,并假定编号和名称均与“漂亮人物”制造 - 那些没有意义CMD
的解析器即。字母(大小写)和数字,并设定[@#$+_-{}:.]
注意这个非常明确地排除空间,标签和逗号,也该批次很少做关于案件的区别....
哦 - 运行 - 你问?
呃0.63秒
请出示第三文件的有代表性的样品,并显示你从其他2。我把它从你的描述是file1中具有相应的行ID和姓名的光处理的第三个文件会发生什么来自file2的是实际的名称,用于代替ID在file3中出现的任何ID。那是对的吗? – Magoo
是的,这是正确的。 ID.txt和Name.txt在确切的行号上有相应的文本。 Data.txt在整个区域都有一堆ID字符串,我们只需要将这些字符串翻译成名称。 – Kaillera
好吧,我改变了主要帖子,以符合你问的标准。 – Kaillera