2014-12-28 62 views
0

我有一点障碍,试图为社区做点什么,我花时间陪伴,我需要帮助。我是新的.bat和所有。使用两个文件用蝙蝠替换另一个文件中的文本?

我有3个文件。

1)标识 2)与的ID 3)用一堆文本文件专有名称列表的文件列表的文件,包含ID的随机在它所有的地方。

我想使用ID和名称来替换第三个文件中的ID。第一个和第二个文件如下所示:

ID.txt 
====== 
001_Blue019 
002_Bluer11 
003_Buster142 

Name.txt 
====== 
Bob Blue 
Bluer Baxster 
Buster Arnold 

所有内容都完美对齐。我想使用这两个文件来更改第三个文件,其中包含随机放置在整个文本文件中的ID,一些ID可能会多次出现。我遇到了麻烦,我该如何做到这一点?

第三个文件看起来是这样,但出现不止一次像500不同的ID:

001_Blue019 
001_Blue019 
001_Blue019 
002_Bluer11 
001_Blue019 
001_Blue019 
003_Buster142 

最终的输出或改变应该像

Bob Blue 
Bob Blue 
Bob Blue 
Bluer Baxster 
Bob Blue 
Bob Blue 
Buster Arnold 
+0

请出示第三文件的有代表性的样品,并显示你从其他2。我把它从你的描述是file1中具有相应的行ID和姓名的光处理的第三个文件会发生什么来自file2的是实际的名称,用于代替ID在file3中出现的任何ID。那是对的吗? – Magoo

+0

是的,这是正确的。 ID.txt和Name.txt在确切的行号上有相应的文本。 Data.txt在整个区域都有一堆ID字符串,我们只需要将这些字符串翻译成名称。 – Kaillera

+0

好吧,我改变了主要帖子,以符合你问的标准。 – Kaillera

回答

0
@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秒

+0

这正是我需要Magoo的解决方案。 .bat在转换时相当缓慢,但是它可以创造奇迹。非常感谢! – Kaillera

0

如果你的第三个文件包含的无非是标识,如在你的例子所示,那么下面的批处理脚本应该是相当快:

@echo off 
setlocal enableDelayedExpansion 

:: Load the list of IDs 
set "find=" 
<name.txt (for /f "usebackq delims=" %%A in ("id.txt") do (
    set "name=" 
    set /p "name=" 
    set "_%%A=!name!" 
)) 

for /f "usebackq delims=" %%A in ("test.txt") do echo(!_%%A! 

如果在第三文件的ID与其他文本混合,那么我有一个不同的快速解决方案,只要您没有超过四五百个ID /名称对即可。它使用JREPL.BAT - 一种混合的JScript /批处理脚本,它可以从XP以后的任何Windows机器上本机运行。

@echo off 
setlocal enableDelayedExpansion 

:: Load the list of IDs 
set "find=" 
for /f "usebackq delims=" %%A in ("id.txt") do set "find=!find!|%%A" 
set find 

:: Load the list of Names 
set "repl=" 
for /f "usebackq delims=" %%A in ("name.txt") do set "repl=!repl!|%%A" 
set repl 

:: Substitute Names for all IDs within test.txt and write the result to out.txt 
call jrepl find repl /l /t "|" /v /f test.txt /o out.txt 
+0

我有大约500多对ID和名称是。还需要保留其他原始文本的一部分。 – Kaillera

相关问题