@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的数据将数据文件中的行数扩展到10000多行,并测量了上述过程。在我的机器上花了 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
相同的结果文件,它假定 ID 和名称都是由“好字符”组成的——那些对CMD 的解析器没有意义的字符,即。字母表(大写和小写)和数字以及集合[@#$+_-{}:.] 请注意,这非常明确地排除了 Space、Tab 和逗号,而且该批次很少区分案例....
哦 - 运行时,你问?
呃,0.63 秒。