我如何使用批处理命令搜索“//packages/thirdparty/release/main.txt #10”并将#10 替换为#15(set VARIABLE="#15")以及忽略空格。#10 可以是任意值,让我们假设#XXX。 最好使用命令行示例。
测试日志
//packages/thirdparty/release/config.txt #8
//packages/thirdparty/release/config2.txt #3
//packages/thirdparty/release/config1.txt #4
//packages/thirdparty/release/main.txt #10
这是我的尝试
@ECHO OFF
set $FindStr=//packages/thirdparty/release/main.txt#10
set $ReplString=//packages/thirdparty/release/main.txt#15
setlocal enabledelayedexpansion
for /f "delims=" %%a in ('type testlog.txt') do (
set $Ver=%%a
set $Ver=!$Ver: =!
If /i !$Ver!==%$FindStr% set $Ver=%$ReplString%
echo !$Ver! ) >> testlog.txt
答案1
您可以使用复制命令脚本来执行此操作。只需保存并像这样运行它:
type testlog.txt|repl.bat "#\d\d?$" "#15"
如果你只想替换某个数字,那么你只需要稍作修改:
type testlog.txt|repl.bat "#10$" "#15"
如果您想要更好地控制事物的替换方式,那么您可以使用另一个脚本:
@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
COPY /A testlog.txt textlog.out.txt >nul
SET VARIABLE=0
FOR %%S IN ( 1 3 4 8 10 20 ) DO (
SET /A VARIABLE=!VARIABLE!+1
COPY /A textlog.out.txt textlog.tmp.txt >nul
TYPE textlog.tmp.txt|repl.bat "#%%S$" "#!VARIABLE!" > textlog.out.txt
)
DEL textlog.tmp.txt
ENDLOCAL