【问题标题】:batch file to replace a line using sed inside a for loop doesn't work在 for 循环中使用 sed 替换行的批处理文件不起作用
【发布时间】:2013-12-21 01:26:35
【问题描述】:
setlocal EnableDelayedExpansion
for /L %%g in (1,1,100) do (
  set /a c=%%g+1
  echo !c!
  echo c:\cygwin\bin\sed.exe -e 's/Token="%%g"/Token="!c!"/g' xyz.xml > xxx_!c!.xml
)
pause

我想在 xml 文件中将 Token="n" 替换为 Token="n+1"。上面的脚本不起作用。 有什么帮助吗?

【问题讨论】:

  • 您能描述一下您的 .bat 的最终结果吗?我猜这与"!c!" 没有正确扩展有关,但目前还很难说。

标签: loops math batch-file for-loop sed


【解决方案1】:
setlocal EnableDelayedExpansion
(
 for /L %%g in (100,-1,1) do (
   set /a c=%%g+1
   echo !c! >con:
   echo s/Token="%%g"/Token="!c!"/g
 )
)>xxx.sed
c:\cygwin\bin\sed.exe -f xxx.sed xyz.xml > xxx.xml

应该看到你的权利。我不熟悉cygwin/sed 语法;创建的文件xxx.sed 应包含适当的 SED 指令以进行更改(对于 n=1 到 100,将 [Token="n"] 替换为 [Token="n+1"])

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-08-26
    • 1970-01-01
    • 2021-02-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-07
    相关资源
    最近更新 更多