在不同的文本行之间插入文本行

时间:2015-05-09 22:02:19

标签: batch-file cmd insert windows-7-x64

我想在不同的文本行之间插入文本行,例如:

我在Windows 7 x64上运行。

我有一个名为Lines2Insert.txt的文件,该文件包含数千行文字,如下所示:

WRAITH_CAPITALBATTLESHIP                 
WRAITH_CAPITALCARRIER             
WRAITH_CAPITALCOLONY                                                               
WRAITH_CAPITALSIEGE              
WRAITH_CAPITALSUPPORT             
WRAITH_CAPITALTRANSPORT  
WRAITH_CRUISERANTIMODULE                                                                     

我想在这一行文本之间插入两行文本,该文本位于不同的文件中,如下所示。

StringInfo  
    ID "IDS_(This is where I want it inserted)_NAME"             
    Value "TODO"

StringInfo  
    ID "IDS_(This is where I want it inserted)_DESCRIPTION"                                   
    Value "TODO"

所以我希望最终结果如下:

StringInfo  
    ID "IDS_WRAITH_CAPITALBATTLESHIP_NAME  
    Value "TODO"

StringInfo  
ID "IDS_WRAITH_CAPITALBATTLESHIP_DESCRIPTION"  
Value "TODO"

提前致谢,对不起,如果我只是困惑你,我会在将来尝试更清楚。

最好想要一个代码为

.bat文件

1 个答案:

答案 0 :(得分:1)

你的问题很混乱。如果要将每个字符串插入两次,则应在第三次出现替换字符串时发布所需输出的示例。无论如何,我认为这是解决方案:

@echo off
setlocal EnableDelayedExpansion

set "twice="
< Lines2Insert.txt (
   set /P insert=
   for /F "tokens=1* delims=:" %%a in ('findstr /N "^" input.txt') do (
      set "line=%%b"
      if defined line (
         for /F %%i in ("!insert!") do set "lineMod=!line:(This is where I want it inserted)=%%i!"
         if "!line!" neq "!lineMod!" (
            if not defined twice (
               set twice=yes
            ) else (
               set "twice="
               set /P insert=
            )
         )
         echo(!lineMod!
      ) else (
         echo/
      )
   )
) > modFile.txt
move /Y modFile.txt input.txt

将此文件用作 input.txt

StringInfo
    ID "IDS_(This is where I want it inserted)_NAME"
    Value "TODO"

StringInfo
    ID "IDS_(This is where I want it inserted)_DESCRIPTION"
    Value "TODO"

StringInfo
    ID "IDS_(This is where I want it inserted)_OTHERDATA"
    Value "TODO"

结果

StringInfo
    ID "IDS_WRAITH_CAPITALBATTLESHIP_NAME"
    Value "TODO"

StringInfo
    ID "IDS_WRAITH_CAPITALBATTLESHIP_DESCRIPTION"
    Value "TODO"

StringInfo
    ID "IDS_WRAITH_CAPITALCARRIER_OTHERDATA"
    Value "TODO"