1

textfile1 是源,textfile2 是目标

示例 textfile1.txt:

  server1.net 2100 /l /n /k port:2000,server2.net 2100 /l /n /k port:20000

文本文件2.txt:

  server3.net 2000 /l /k port:xxxx,server4.net 2000 /l /k port:yyyyy

我需要在 textfile1.txt 中找到“port:”字符并在 textfile2.txt 中替换端口号(“port:”字符后的 n 个字符)

注意:portnumber 是 textfile1 中的变量号,但 text: "port:" 是固定的。谢谢

基本上我需要在 textfile1 和 textfile2 中放置相同的端口号

4

3 回答 3

1

下面的批处理文件适用于两个文本文件中的无限服务器列表,并允许port:nnnn在行中的任何位置包含该选项。它不检查任何错误以使其更简单。

@echo off
setlocal EnableDelayedExpansion
rem Read data from both files
set /P "data1=" < textfile1.txt
set /P "data2=" < textfile2.txt
rem Replace "port" data in all servers
:nextServer
   rem Get port number after "port:" in first data and eliminate it
   for /F "tokens=1* delims=," %%a in ("%data1:*port:=%") do set port1=%%a& set data1=%%b
   rem Replace port number in second data and change colon by semicolon
   for /F "tokens=1  delims=," %%a in ("%data2:*port:=%") do set data2=!data2::%%a=;%port1%!
rem Pass to next server, if any
if defined data1 goto nextServer
rem Output result restoring colons
echo %data2:;=:%

例如,使用此数据:

  server1.net 2100 /l /n /k port:2000,server2.net 2100 /l /n /k port:20000,server5.net 2100 /l /n /k port:1234

server3.net 2000 /l /k port:xxxx,server4.net 2000 /l /k port:yyyyy,server6.net 2000 /l /k port:zzzz

输出是:

server3.net 2000 /l /k port:2000,server4.net 2000 /l /k port:20000,server6.net 2000 /l /k port:1234
于 2012-11-11T06:58:57.313 回答
1

我不明白您为什么要使用批处理来执行此任务 - 使用另一种脚本语言(如 VBScript、JScript 或 PowerShell)会更简单。

但这是一个原生批处理解决方案。我的解决方案不假设每个命令中选项的顺序。它允许之前和之后的任意数量的选项/port:nnnn

@echo off
setlocal enableDelayedExpansion

::read the line from textfile1
set "str="
<"textfile1.txt" set /p "str="
if not defined str exit /b

::enclose each command in quotes
set "str="!str:,=","!""

::extract the port from each command into an array
set /a n=0
for %%A in (!str!) do (
  set "cmd=%%~A"
  set /a n+=1
  for /f %%B in ("!cmd:*port:=!") do set "port!n!=%%B"
)

::read the line from textfile2
set "str="
<"textfile2.txt" set /p "str="
if not defined str exit /b

::enclose each command in quotes
set "str="!str:,=","!""

::process each command
set /a n=0
set "ln="
for %%A in (!str!) do (
  set "cmd=%%~A"
  REM break the command into 2 parts, discarding port:
  REM %%B = before port
  REM %%E = after port
  for /f "tokens=1* delims=," %%B in ("!cmd:port:=,!") do (
    for /f "tokens=1*" %%D in ("%%C") do (
      set /a n+=1
      REM transfer !n! into %%N
      for %%N in (!n!) do (
        REM build the new line
        set "ln=!ln!,%%B port:!port%%N! %%E"
      )
    )
  )
)

::remove space before comma
set "ln=!ln: ,=,!"

::write the result back to textfile2
>"textfile2.txt" echo !ln:~1!
于 2012-11-10T20:57:03.317 回答
0

进一步澄清您希望长期做什么可能会有所帮助,但与此同时,这是我刚刚编写和测试的一个脚本,它完全符合您的要求。

for /f "tokens=6,12 delims=, " %%a in (textfile1.txt)do (
set portOne=%%a
set portTwo=%%b
call :writeTextTwo
)
type texttwo_temp>textfile2.txt
goto :EOF
:writeTextTwo
for /f "tokens=1-12 delims=, " %%c in (textfile2.txt)do (echo %%c %%d %%e %%f %portOne%,%%h %%i %%j %%k %portTwo% >>texttwo_temp )
 goto :EOF

我希望这有帮助。

于 2012-11-10T14:03:30.050 回答