2013-05-20 66 views
0

使用批处理文件。批处理文件将特定行从conf文件复制到另一个conf文件

之间的<username>dynamic_username</username>标签text1.conf是一个动态的用户名字符串。我希望它会自动被选中并复制到另一个文件text2.conf和替换空白标签<username>insert here username</username>

text1.conf包含<username>dynamic_username</username> < - 它的第19行

text2.conf包含<username></username> < - 其第10行

在此先感谢

回答

0

试试这个:

for /f "tokens=2delims=<>" %%i in ('findstr /i "username" "text1.conf"') do set "string=%%i" 
(for /f "delims=" %%i in ('findstr /n "^" "text2.conf"') do (
    set "line=%%i" 
    setlocal enabledelayedexpansion 
    set "line=!line:*:=!" 
    if "!line!" neq "!line:username=!" set "line=%string%" 
    echo(!line! 
    endlocal 
))>"text2.conf.new" 
0
@ECHO OFF 
SETLOCAL 
(
FOR /f "delims=" %%i IN (text2.conf) DO (
IF "%%i"=="<username></username>" (
    FINDSTR /b /e ".*<username>.*</username>.*" <text1.conf 
) ELSE (ECHO(%%i) 
) 
)>text2.conf.new 

FC text2.conf text2.conf.new 

GOTO :eof 

虽然会删除空行...

相关问题